Implementation of the five-merge sort in vernacular classical algorithm series

Source: Internet
Author: User

Merge sort is an efficient sorting algorithm based on the merging operation. This algorithm is a typical application of the method of division (Divide and Conquer).

First consider how to combine the two ordered series. This is very easy, just from the comparison of the first number of two series, who is small first to take who, after taking the corresponding number of the column deleted. Then the comparison, assuming that there are several columns empty, then there will be a sequence of data in turn to take out.

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 & amp;& J < m) {if (A[i] < b[j]) c[k++] = a[i++];elsec[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).

Conquer the above merge ordered sequence of problems, 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 very convenient to the two groups of data to sort. How do you get the data in the two groups in order?

, then sequence is complete. merge sort.

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;in t k = 0;while (i <= m && J <= N) {if (A[i] <= a[j]) temp[k++] = a[i++];elsetemp[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, fi RST, Mid, temp);    Left ordered MergeSort (A, mid + 1, last, temp); Right ordered Mergearray (a, first, mid, last, temp); Merge two ordered series}}bool mergesort (int a[], int n) {int *p = new Int[n];if (p = = NULL) return False;mergesort (A, 0, n-1, p);d ele Te[] P;return true;}

The efficiency of merge sorting is relatively high, set the number of series is N, divide the series into small series to Logn step, each step is a process of merging ordered series, time complexity can be recorded as O (N), so altogether O (N*logn). Since the merge sort is performed in the adjacent data each time, several sorting methods (high-speed sorting, merge-sort, hill-sort, heap-sort) of the merge sort in O (N*logn) are also highly efficient.

On my computer on the bubble sort, direct insertion sort, merge sort and directly use the system qsort (all in the release version number)

Test for 20,000 random data:

Test for 50,000 random data:

Test the 200,000 random data:

Note: Some books allocate a temporary array when Mergearray () merges an ordered series, but too many new operations can be time-consuming. So a little change has been made. Only the new temporary array in MergeSort (). This temporary array is shared by all subsequent operations.

Implementation of the five-merge sort in vernacular classical algorithm series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.