Merge sort is an efficient sorting algorithm based on the merging operation. This algorithm is a very typical application of the partition method (Divide and Conquer).
First consider how to combine the two ordered series. This is very simple, as long as the first number from the comparison of two series, who is the first to take who, after taking the corresponding sequence of the deletion of this number. Then the comparison, if there are several columns empty, then directly the data of the other sequence can be taken out in turn.
[CPP]View Plaincopy
- Merge ordered Arrays a[] and b[] into c[]
- void Memeryarray (int a[], int n, int b[], int m, int c[])
- {
- int I, j, K;
- i = j = k = 0;
- While (i < n && J < m)
- {
- if (A[i] < b[j])
- c[k++] = a[i++];
- Else
- c[k++] = b[j++];
- }
- While (i < n)
- c[k++] = a[i++];
- While (J < m)
- c[k++] = b[j++];
- }
It can be seen that the efficiency of the combined ordered series is relatively high and can reach O (n).
Solve the above merge ordered series, and then look at the merger sort, the basic idea is to divide the array into two groups, a, if the two groups of data are ordered, then it is convenient to the two groups of data to sort. How do you get the data in the two groups in order?
A and B groups can be divided into two groups respectively. And so on, when the group has only one data, it can be thought that the group has reached an orderly, and then merge the adjacent two groups. This is done by first recursive decomposition of the sequence, and then the sequence is completed by the merge sequence.
[CPP]View Plaincopy
- There will be two ordered series A[first...mid] and A[mid ... Last] Merge.
- void Mergearray (int a[], int first, int mid, int last, int temp[])
- {
- int i = First, j = mid + 1;
- int m = Mid, n = last;
- int k = 0;
- While (i <= m && j <= N)
- {
- if (A[i] <= a[j])
- temp[k++] = a[i++];
- Else
- temp[k++] = a[j++];
- }
- While (i <= m)
- temp[k++] = a[i++];
- While (j <= N)
- temp[k++] = a[j++];
- For (i = 0; i < K; i++)
- A[first + i] = temp[i];
- }
- void MergeSort (int a[], int first, int last, int temp[])
- {
- if (first < last)
- {
- int mid = (first + last)/2;
- MergeSort (A, first, mid, temp); //Left order
- MergeSort (A, mid + 1, last, temp); //Right order
- Mergearray (A, first, mid, last, temp); //Merge two sequential series
- }
- }
- BOOL MergeSort (int a[], int n)
- {
- int *p = new int[n];
- if (p = = NULL)
- return false;
- MergeSort (A, 0, n-1, p);
- delete[] p;
- return true;
- }
The efficiency of the merge sort is relatively high, set the sequence length is N, divide the series into small series to Logn step, each step is a process of merging ordered series, the time complexity can be recorded as O (N), so altogether O (N*logn). Because the merge sort is done in the adjacent data each time, several sorting methods (quick sorting, merge sort, hill sort, heap sort) of the merge sort in O (N*logn) are also relatively efficient.
In My computer on the bubble sort, direct insertion sort, merge sort and directly use the system qsort () to compare (all in release version)
Test 20,000 random data:
Test 50,000 random data:
Then test the 200,000 random data:
Note: Some books allocate a temporary array when Mergearray () merges an ordered series, but excessive new operations can be very time consuming. So a little change has been made. Only a temporary array of new in MergeSort (). This temporary array is shared by all subsequent operations.
Implementation of the five-merge sort in vernacular classical algorithm series