合并排序算法排序过程_合并排序| 用于大型输入的最佳排序算法之一
合并排序算法排序過程
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...
讓我們快速跳到編碼部分...
.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 caseif(s>=e)return ;///recursive casemergesort(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
合并排序算法排序過程
總結
以上是生活随笔為你收集整理的合并排序算法排序过程_合并排序| 用于大型输入的最佳排序算法之一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全身体检需要多少钱啊?
- 下一篇: c语言打印数组元素_C程序打印元素差为0