合并排序算法排序过程
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,
在编程中,有许多排序算法,下面给出其中一些,
Bubble sort
气泡排序
Selection sort
选择排序
Insertion sort
插入排序
Merge sort
合并排序
Quick sort
快速分类
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个简单步骤即可对数组进行排序:
Divide the array into two parts
将数组分为两部分
Recursively sort both the parts
对两个部分进行递归排序
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...
让我们快速跳到编码部分...
#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
合并排序算法排序过程