合并排序算法排序过程

What is sorting?

什么是分类?

Sorting allows us to process our data in a more organized and efficient way. It makes searching easy as it will now take less time to search for a specific value in a given sorted sequence with respect to a sequence which was initially unsorted.

排序使我们能够以更有条理和有效的方式处理数据。 它使搜索变得容易,因为相对于最初未排序的序列,现在将花费更少的时间在给定的排序序列中搜索特定值。

In programming, there are any numbers of sorting algorithms, some of them are given below,

在编程中,有许多排序算法,下面给出其中一些,

  1. Bubble sort

    气泡排序

  2. Selection sort

    选择排序

  3. Insertion sort

    插入排序

  4. Merge sort

    合并排序

  5. Quick sort

    快速分类

  6. Randomized Quick sort (an optimized quick sort)

    随机快速分类(一种优化的快速分类)

But, the problem with such sorting algorithms like bubble sort, insertion sort, and the selection sort is they take a lot of time to sort.

但是,诸如气泡排序,插入排序和选择排序之类的排序算法的问题在于它们需要大量时间来排序。

For example, If we have to sort an array of 10 elements then any sorting algorithm can be opted but in case of an extensively high value of N that is the no. of elements of the array like if N=1000000 then in case the starting 3 sorting algorithms cannot be opted as the time they will take is proportional to (N*N) which in big O notation can be represented as O(N*N).

例如,如果我们必须对10个元素的数组进行排序,则可以选择任何排序算法,但是如果N的值很高,则为否。 数组的元素,例如,如果N = 1000000,则万一开始的3种排序算法无法选择,因为它们花费的时间与(N * N)成正比,则在大O表示中可以表示为O(N * N)

So today we will focus on a more optimized sorting algorithm that is (MERGE SORT).

因此,今天我们将重点放在一种更优化的排序算法( MERGE SORT )上。

Basically, both merge and quick sort are divide and conquer algorithms.

基本上,合并和快速排序都是分治算法

But today we'll be focusing on MERGE SORT and the main reason of casting light upon this sorting algorithm is it takes O(N*logN) time which is very efficient with respect to the O(N*N).

但是今天,我们将重点讨论MERGE SORT,并且将光线投在此排序算法上的主要原因是,它需要O(N * logN)时间,这相对于O(N * N)非常有效。

In merge sort we follow just 3 simple steps to sort an array:

在合并排序中,我们仅需3个简单步骤即可对数组进行排序:

  1. Divide the array into two parts

    将数组分为两部分

  2. Recursively sort both the parts

    对两个部分进行递归排序

  3. Then, merge those two stored parts into one

    然后,将这两个存储的部分合并为一个

Description:

描述:

So basically it is considered as one of the best sorting algorithms having a worst case and best case time complexity of O(N*Log(N)), this is the reason that generally we prefer to merge sort over quicksort as quick sort does have a worst-case time complexity of O(N*N).

因此,基本上,它被认为是O(N * Log(N))的最坏情况和最坏情况下时间复杂度最好的排序算法之一,这就是为什么我们通常更喜欢像快速排序那样合并排序而不是快速排序O(N * N)的最坏情况下的时间复杂度。

Let’s quickly jump upto the coding part...

让我们快速跳到编码部分...

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }
#include<iostream>
using namespace std;
int temp[10000];
void mergearrays(int ar[],int s,int e)
{int mid=(s+e)/2;
int i,j;
i=s;  j=mid+1;
int x=s;
while(i<=mid&&j<=e)
{if(ar[i]<ar[j])
{temp[x++]=ar[i];
i++;
}
else
{temp[x++]=ar[j];
j++;
}
}
while(i<=mid)
{temp[x++]=ar[i];
i++;
}
while(j<=e)
{temp[x++]=ar[j];
j++;
}
for(int i=s;i<=e;i++)
ar[i]=temp[i];
}
void mergesort(int ar[],int s,int e)
{int mid=(s+e)/2;
///base case
if(s>=e)
return ;
///recursive case
mergesort(ar,s,mid);
mergesort(ar,mid+1,e);
mergearrays(ar,s,e);
}
int main()
{int n;
cout<<"Enter total number of elements: ";
cin>>n;
int ar[10000];
cout<<"The unsorted array is (Enter elements): "<<endl;
for(int i=0;i<n;i++)
cin>>ar[i];
mergesort(ar,0,n-1);
cout<<"The sorted array is"<<endl;
for(int i=0;i<n;i++)
cout<<ar[i]<<" ";
return 0;
}

Output

输出量

Enter total number of elements: 7
The unsorted array is (Enter elements):
7 4 6 5 3 2 1
The sorted array is
1 2 3 4 5 6 7

翻译自: https://www.includehelp.com/algorithms/merge-sort-one-of-the-best-sorting-algorithms-used-for-large-inputs.aspx

合并排序算法排序过程

合并排序算法排序过程_合并排序| 用于大型输入的最佳排序算法之一相关推荐

  1. 帆软相同列合并_合并表格工具_合并表格工具的痛点_合并表格工具哪一款比较好用-帆软...

    在制作数据表格的时候,经常需要将一些表格合并.使用普通的办公软件合并表格是比较麻烦的.不过,现在市面上有不少合并表格工具,到底哪一款比较好用呢? 1.合并表格工具的痛点有哪些? 在制作数据表格的时候, ...

  2. 排序算法python实现_合并排序算法– Java,C和Python实现

    排序算法python实现 Merge sort is one of the most efficient sorting algorithms. It works on the principle o ...

  3. 排序算法大集锦_合并排序_1(分治思想)

    这一系列博客的特点就是--给出每趟排序的结果 本来想着好好写一下过程,弄个图片什么的,不过觉得网上的解析太多了,都比较好,所以这些博客就算是对自己的总结吧. #include <stdio.h& ...

  4. 希尔排序的详细过程_算法系列: 10大常见排序算法(4)希尔排序

    本课程是从少年编程网转载的课程,目标是向中学生详细介绍计算机比赛涉及的编程语言,数据结构和算法.编程学习最好使用计算机,请登陆 www.3dian14.org (免费注册,免费学习). 一句 希尔排序 ...

  5. 语言 排序后重置索引_当C++操作符重载、模板遇到排序(一)

    本文主要是使用C++的操作符重载和模板的语法,完成一些结构性数据的排序,本文会实现冒泡.插入和选择三种简单排序,后续会更新希尔排序.快速排序.归并排序三种高级简单排序的方法. 1.C++操作符重载 操 ...

  6. python 字符串排序 偶数位交换_在Python中将字符串列表转换为整数排序列表

    当我们必须处理以字符串表示的数字时,使用python分析数据可以为我们带来情景.在本文中,我们将获取一个列表,其中包含以字符串形式出现的数字,我们需要将其转换为整数,然后以排序方式表示它们. 带图和排 ...

  7. mysql的字符集和排序规则怎么选择_关于MySQL创建数据库字符集和数据库排序规则的对比选择...

    一.字符集 一般选择utf8.下面介绍一下utf8与utfmb4的区别. utf8mb4兼容utf8,且比utf8能表示更多的字符.至于什么时候用,看你的做什么项目了,unicode编码区从1 - 1 ...

  8. python合并两个有序列表_合并两个有序链表(Python3)

    提出问题:将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-& ...

  9. python合并word表格单元格_合并Word 表格中单元格

    //合并Word 表格中单元格 procedure mergeWordCell; var WordApp: TWordApplication; WordDoc: TWordDocument; DocI ...

最新文章

  1. 刷新aspx页面的六种方法
  2. Java Scanner 类
  3. layui对json数据的格式要求
  4. Scala之部分应用函数
  5. 安装fastdfs出现/usr/local/lib/libfastcommon.so: undefined reference to `fcntl64@GLIBC_2.28‘
  6. Zing加快了JVM应用程序的预热
  7. c++编码风格指南_100%正确编码样式指南
  8. 领域应用 | 知识图谱在小米的应用与探索
  9. mysql如何进行分组操作的_mysql group by 对多个字段进行分组操作
  10. 无线上网卡和无线网卡的区别
  11. jsp分页代码mysql_jsp分页(jsp分页完整代码)
  12. java 控制层和业务层,控制层、业务层和数据访问层
  13. mysql 5.7 临时表_MySQL 5.7内部临时表使用
  14. vcf格式(vCard)转成excel的操作方法
  15. 智铺子微信扫码点餐系统有哪些可操作的功能
  16. Serverless实战——2分钟,教你用Serverless每天给女朋友自动发土味情话
  17. 第四周项目3---单链表的应用之连接
  18. 从零双排java之Map
  19. OpenCV中稠密光流算法总结
  20. 源码学习之Vector

热门文章

  1. java 文件上传 servlet_java文件上传-原始的Servlet方式
  2. extjs 基础部分
  3. 中后端管理系统前后分离、前端框架的实现拙见
  4. 如何用纯 CSS 创作一个冒着热气的咖啡杯
  5. 关于CSS3实现响应式布局的一些概念和术语
  6. 深入理解line-height与vertical-align——前端布局常用属性
  7. iOS中Safari浏览器select下拉列表文字太长被截断的处理方法
  8. 《软件工程导论》课后习题解答
  9. CentOS7 安装NodeJS
  10. jquery jgrid filterToolBar beforeSearch 修改postData