Sorting Algorithm Summary (4) -- Merge Sorting and base sorting

Source: Internet
Author: User

First 3Article:

Sorting Algorithm Summary (3) -- exchange sorting

Sorting Algorithm Summary (2) -- select sorting

Sorting Algorithm Summary (1) -- insert sorting

Iv. Merge Sorting

The same as the Quick Sort, merge sort is achieved using the grouping policy.AlgorithmThe algorithm can be implemented recursively. The so-called merge is to combine two or more ordered data sequences into a new ordered data sequence.

Assuming that the array has n elements, it can be seen that the array is composed of N ordered subsequences. The length of each subsequence is 1 and then merged by two, we obtain an ordered subsequence of n/2 with a length of 2 or 1, and then merge them by two. This is repeated until we get an ordered data sequence with a length of N, the Merge Sorting method is called the two-way Merge Sorting method.

It can be written using iterative ideas.Code:

Void Merge (int * a, int N, int * temp, int K) {// K is the length of the ordered array // an ordered sub-sequence after a two-way Merge Sorting is stored in the array temp int head1 = 0; // the start position of the previous array int head2, tail1, tail2; // indicates the first and second sorted self-sequence int I, j, ntemp = 0; while (head1 + k <n-1) {head2 = head1 + k; // the starting position of the second sequence tail1 = head2-1; // The End of the first self-sequence tail2 = (head2 + k-1 <= n-1 )? (Head2 + k-1): n-1; // the end of the second self-sequence // the combination of the two ordered self-sequences for (I = head1, j = head2; I <= tail1 & J <= tail2; ntemp ++) {if (a [I] <= A [J]) {temp [ntemp] = A [I]; I ++;} else {temp [ntemp] = A [J]; j ++ ;}// after merging auto-sequence 2, place sequence 1 behind the temporary array while (I <= tail1) {temp [ntemp] = A [I]; I ++; ntemp ++ ;} // after merging auto-sequence 1, put auto-sequence 2 behind the temporary array while (j <= tail2) {temp [ntemp] = A [J]; j ++; ntemp ++;} head1 = tail2 + 1;} // put only one group of data elements in the temporary array for (I = head1; I <n; I ++, ntemp ++) temp [ntemp] = A [I];} void mergesort (int * a, int N) {int I, k = 1; // K indicates that the merging length starts from 1 int * temp; temp = (int *) malloc (N * sizeof (INT )); // dynamically apply for temporary array space while (k <n) {Merge (A, N, temp, k); // call the merging function for (I = 0; I <N; I ++) A [I] = temp [I]; // elements from the temporary array are put back to the original array K = 2 * K; // double the merge length} Free (temp );}

5. Base sorting

The basic idea of the base sorting algorithm is: Set the key word of the data element to be sorted to a m-bit D-hexadecimal INTEGER (if the keyword is less than m-bit, fill 0 in the high position), set D buckets, the numbers are 0, 1, 2 ,..., d-1. First, each data element is placed in the corresponding bucket according to the value of the keyword delimiter. Then, the data elements allocated to each bucket are collected in the order of the data elements in the bucket number from small to large. In this way, a new arrangement of the Data Element Set is formed, this sorting process is called a base sort. Then, the sequence of data elements obtained from a base sorting order is placed in the corresponding bucket by the value at the low level of the keyword, then, the data elements allocated to each bucket are collected in the order of data elements in the bucket numbers from small to large. This process is repeated. After the M-level base sorting is completed, the sorted data element sequence is obtained.

Is an example of sorting {710,342,045,686,006,841,429,134,068,264.

Chained queue is a common method for sorting the base.

In the chain queue-based base sorting algorithm, d queues can be designed as a queue array (set the queue array to Tub). Each element of the queue array includes two fields: front domain and rear domain. The front field is used to indicate the team header, and the rear field is used to indicate the team end. When I (I = 0, 1, 2 ,.., D 1) when there are data elements in a queue to be put in, a node is inserted at the end of the corresponding element tub [I] in the queue array. Storage Structure Based on the chained queue base sorting algorithm.

Code omitted.

Vi. Performance Comparison

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.