Summary of several sorting algorithms (II)

Source: Internet
Author: User

Reprinted please indicate the source

Http://blog.csdn.net/pony_maggie/article/details/36706131

Author: Pony



Yishile sorting

In the previous article, we talked about direct insertion sorting, time complexity O (N ^ 2). Please think about its process in your mind. If a sequence is originally ordered, the time complexity of sorting it is O (n ). Therefore, when the sequence is basically ordered, the efficiency of insertion sorting is greatly improved because the movement is reduced.

 

In addition, direct insertion sorting also features a high efficiency when n is relatively small.

 

Hill sorting is an improved algorithm based on the above two ideas. It first divides the whole sequence into several small sequences and performs direct insertion and sorting for each small sequence, so that the whole sequence becomes "Basic Order" and then performs direct insertion and sorting for the whole sequence, get the final result. However, Hill sorting does not simply segment by segment, but uses records separated by an increment to form a sequence. As shown in:


 


 


At the beginning, the increment is 3, which has three groups: {9, 8, 4}, {1, 3, 6}, {5, 7, 2}, respectively. insert and sort them to get the 2 chart, then increase to 2, continue the above process, and when the increment is 1, the array is ordered. An incremental sequence {, 2, 1} is constructed using the increment values of the trigger sorting }. This is not fixed, but a good incremental sequence should have no common factor except 1, and the last increment must be equal to 1. The idea is clear. Go to the code.

 

// One-Stop insertion sorting. dk is a single incremental static void shellinsert (INT narray [], int nlength, int DK) {int I = 0; Int J = 0; int nserity = 0; for (I = dk; I <nlength; I ++) {If (narray [I] <narray [I-dk]) {nserity = narray [I]; for (j = I-DK; (j> = 0) & (nserity <narray [J]); j-= dk) {narray [J + dk] = narray [J];} narray [J + dk] = nserity ;}} int shellsort (INT narray [], int nlength) {int K = 0; int dkarray [] = {3, 2, 1}; // The default incremental sequence int dklength = 3; for (k = 0; k <dklength; k ++) {shellinsert (narray, nlength, dkarray [k]);} return 0 ;}

The computation of its complexity involves some mathematical difficulties. You only need to know that its efficiency is relatively higher than that of direct insertion and sorting.

 

Ii. Quick sorting

 

In some cases, quick sorting is an improvement for Bubble sorting. I don't think it is so much association that will mislead you into learning quick sorting.

 

The idea of fast sorting first selects a "pivot element", which is generally the first element of the sequence. Put data smaller than this hub on one side, and put data larger than it on the other side. After such a trip, the sequence becomes that all the elements in one part are smaller than those in the other part, but the records between the parts may be unordered. Then we continue to divide each part with the same idea, and finally it becomes an ordered sequence. As shown in:

 


 

Through the above steps, we naturally want to use recursion to achieve fast sorting.

 

static int partition(int nArray[], int nLow, int nHigh){int nPivot = nArray[nLow];while (nLow < nHigh){while ((nLow < nHigh) && (nArray[nHigh] >= nPivot)) nHigh--;nArray[nLow] = nArray[nHigh];while ((nLow < nHigh) && (nArray[nLow] <= nPivot)) nLow++;nArray[nHigh] = nArray[nLow];}nArray[nLow] = nPivot;return nLow;}static void sortProcess(int nArray[], int nLow, int nHigh){int nPartition = 0;if (nLow < nHigh){nPartition = partition(nArray, nLow, nHigh);sortProcess(nArray, nLow, nPartition-1);sortProcess(nArray, nPartition+1, nHigh);}}int quickSort(int nArray[], int nLength){sortProcess(nArray, 0, nLength-1);return 0;}

The time complexity of quick sorting is O (nlogn), which is currently recognized as an efficient sorting algorithm.

 

Tri-Merge Sorting

Merging and sorting is a special sorting algorithm. It combines two ordered tables (M and N respectively) into another ordered table. This action can be performed in O (m + n) time complexity. For an unordered sequence with n elements, we can regard it as N ordered subsequences, and then merge them to get a n/2 length of 2 or 1 (think about why there is 1) to continue to merge the two orders, and get an ordered sequence with a length of N. As shown in:


 

 

Here we use recursive methods to achieve better understanding. Non-recursive methods are more complex. The Code is as follows:

 

// Set the ordered srcarray [I .. m] And srcarray [M + 1 .. n], merged to destarray [I .. n] Static void merge (INT srcarray [], int destarray [], int I, int M, int N) {Int J = 0; int K = 0; for (j = m + 1, K = I; (I <= m) & (j <= N); ++ K) {If (srcarray [I] <srcarray [J]) {destarray [k] = srcarray [I ++];} else {destarray [k] = srcarray [J ++] ;}}// copy the rest directly while (I <= m) {destarray [k ++] = srcarray [I ++];} while (j <= N) {destarray [k ++] = srcarray [J ++];} static void msort (INT srcarray [], int destarray [], int S, int t) {int m = 0; int destarray2 [1, 256] = {0 }; // auxiliary array. The space is allocated according to the actual situation. if (S = T) {destarray [s] = srcarray [s];} else {M = (S + T)/2; msort (srcarray, destarray2, S, m); msort (srcarray, destarray2, m + 1, t); merge (destarray2, destarray, S, M, T );}} // Recursive Method for merging and sorting int mergesort (INT narray [], int nlength) {int ndestarray [256] = {0}; int I = 0; msort (narray, ndestarray, 0, nLength-1); While (I <nlength) narray [I] = ndestarray [I ++]; return 0 ;}

Its time complexity is O (nlog2n ).

 

 

Code:

Http://download.csdn.net/detail/pony_maggie/7568971

Or

Https://github.com/pony-maggie/SortDemo

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.