Super-specific reading basic sorting algorithm (no remorse, with sort demo animation)

Source: Internet
Author: User

Sorting is closely related to our daily life. Example. We're going to find a contact from the phone book. The first is sorted by last name, the train ticket will be sorted according to the departure time or length, the purchase will be sorted according to sales or praise, the search documents will be sorted according to the time of change and so on. In computer programming, sorting and finding are also the most important algorithms, and many other algorithms are based on sorting algorithms, in general data processing or analysis. Usually the first step is to sort, say two-point lookup. The first thing to do is to sort the data. In the four volumes of the Art of computer programming for Donald Knuth. There is a volume that is specifically about sorting and finding.

There are very many algorithms for sorting. There is a category on Wikipedia, and it is also interesting to see the relevant algorithms directly on Wikipedia. This article also participates in the above content.

First look at the simpler selection sort (Selection sort), insert sort (insertion sort), and then on the basis of analyzing the characteristics and drawbacks of the insertion sort. Describes the improved Hill sort (Shell sort) based on the insertion sort.

a selection sort

principle :

Select sort very easy, his process such as the following:

    1. Traverse from left to right to find the smallest (large) element. Then swap with the first element.
    2. Continue looking for the smallest (large) element from the remaining unordered elements. Then swap with the second element.
    3. And so on until all elements are sorted.

It is called selection sorting because each time we traverse an unordered sequence, we always choose the smallest element from it. The following is an animated demonstration of selecting a sort:

Realize:

The algorithm is also very easy to implement. We create a new sort generic class so that the type must implement the IComparable interface, and then we define the Selectionsort method, which passes in the T array. The code is as follows:

// <summary>///sort algorithm generic class, requires type implementation IComparable Interface/// </summary>///<typeparam name= "T" ></typeparam>Public classSort<T>whereT:IComparable<t>{/// <summary> //Select Sort//</summary>// <param name= "Array" ></param>Public static voidSelectionsort (t[] array) {intn = array.        Length;  for(inti = 0; i < n; i++) {            intmin = i; //Start with the first I+1 element and find the minimum value for(intj = i + 1; j < N; j + +) {                if(Array[min].            CompareTo (Array[j]) > 0 min = j; }            //Find and Exchange after element ISwap (array, I, min); }    }    /// <summary> //element Exchange//</summary>//<param name= "Array" ></param>//<param name= "I" ></param>// /<param name= "min" ></param>private static voidSwap (t[] array,intI,intmin)        {T temp = array[i];        Array[i] = Array[min];    Array[min] = temp; }}

The process of selecting each order in the sorting is analyzed, and you can look at the bar chart on the right side of the graph.

Tests such as the following:

static voidMain (string[] args) {    Int32[] array =NewInt32[] {1, 3, 1, 4, 2, 4, 2, 3, 2, 4, 7, 6, 6, 7, 5, 5, 7, 7}; Console. WriteLine ("before Selectionsort:");    PrintArray (array); Sort<Int32.    Selectionsort (array); Console. WriteLine ("after Selectionsort:");    PrintArray (array); Console. ReadKey ();}

Output Result:

Analysis:

Select the sort effect for sorting under various initial conditions such as the following:

    1. The choice of sorting takes (n–1) + (n–2) + ... + 1 + 0 = N (N-1)/2 ~ N2/2 times and N-1 switching operations.

    2. Not sensitive to initial data. Whether or not the initial data is ordered, it is necessary to go through the N2/2, which is a good order for some of the original. Or a sequence that is approximate in order to have no advantage.

      In the best case, that is, the whole sequence, need 0 times exchange, the worst case. Reverse Need N-1 exchange.

    3. Data Exchange is less frequent . Suppose an element is at the correct last position. Then it will not be moved. In the worst case, it is only necessary to carry out N-1 data exchange, in the whole of all rely on exchange to move elements of the sorting method. Choosing a sort is a good one.

Two insert Sort

principle :

Inserting a sort is also a more intuitive way of sorting. It can be explained by the example of our usual playing poker that if the cards we have in hand are in order, then the insertion sort can be understood as the cards we will touch every time. And the hands of the cards from left to right in turn, if you find the right position is inserted directly.

The detailed steps are:

    1. From the beginning of the first element, the element can feel that it has been sorted
    2. Remove the next element. Scan from backward forward in sequenced sequence of elements
    3. Assuming that the element is smaller than the preceding element (sorted), then the comparison with the preceding element is assumed to be less than the interchange. Until it is found that it is larger than the element, stop;
    4. If the element is larger than the previous element (sorted), repeat step 2
    5. Repeat steps until all elements are sorted.

The following is an animated demonstration of inserting a sort:

Realize:

In the sort generic method, we add, for example, the following method, as in the above definition

// <summary>///Insert Sort/// </summary>///<param name= "Array" ></param>Public static voidInsertionsort (t[] array) {intn = array.    Length; //start with the second element for(inti = 1; i < n; i++) {        //From the beginning of the first element, the first and the previous ordered i-1 elements are relatively smaller, assuming less than. The Exchange for(intj = i; j > 0; j--) {            if(Array[j].            CompareTo (Array[j-1]) < 0) {Swap (array, J, j-1); }            Else//assume greater than.                It is not necessary to continue to compare, because the front elements have been ordered, the larger the larger is the teaching of the big.  Break; }    }}

Tests such as the following:

Int32Int32[] {1, 3, 1, 4, 2, 4, 2, 3, 2, 4, 7, 6, 6, 7, 5, 5, 7, 7}; Console . WriteLine ("before Insertionsort:"); PrintArray (array1); Sort < Int32 . Insertionsort (array1); Console . WriteLine ("after Insertionsort:"); PrintArray (array1); Console . ReadKey ();

Output Result:

Analysis:

The sort effect of the insertion sort under various initial conditions such as the following:

1. The average insertion order requires N2/4 and N2/4 times . In the worst case, N2/2 and exchange are required, and in the best case it is only necessary to N-1 and 0 times.

Consider the worst case first, that is, all the elements in reverse order, then the first element needs to be compared with the previous I-1 elements i-1 and Exchange. All add up to be equal to N (N-1)/2 ~ N2/2. In the case of array random arrangement, only need to compare and exchange with the first half of the elements, so the average need N2/4 and N2/4 times Exchange.

In the best case, all elements are ordered, and only the second element starts with the previous element, and does not need to be exchanged. So for the N-1 and 0 times Exchange.

2. In the insert sort, the number of elements exchanged is equal to the logarithm of the reverse element in the sequence. The minimum number of elements is the logarithm of the element in reverse order, up to the number of pairs of elements in reverse order minus 1.

3. Overall. The insertion sort is a more efficient way to sequence a partially ordered sequence and a smaller number of elements.

As in, Sequence aeelmotrxps. The logarithm of the reverse order is t-r,t-p. T-s,r-p. X-s 6 pairs. Typical partial ordered queues are characterized by:

    • Each element in the array is not too far from the end of the sequence.
    • A small unsorted array is added to the large sorted array after
    • Only the individual elements in the array are not sorted.

For partially ordered arrays, the insertion sort is more efficient . When the logarithm of an inverse element in an array is lower, the insertion order is much more efficient than the other sorting methods.

Choose a comparison of sorting and inserting sorts :

Shows the animation effect of inserting sort and selecting sort. The gray pillars in the picture are not moving, the black ones need to be involved in the comparison, and the red ones are in exchange.

It can be seen in the diagram:

Inserting a sort does not move the element to the right. Selecting a sort does not move the element to the left, because the insertion sort involves fewer elements than the inserted element, and involves an average of less than half the selection sort .

three-Hill sort (Shell sort)

Principle:

Hill sort is also known as descending incremental ordering. He is an improvement on the insertion sort. In the second section, insert sort. We know. The insertion sort is for a sequence that is approximately ordered. The efficiency is very high, can achieve the efficiency of linear sequencing. But the insertion sort is also less efficient, and he can only move the data one bit at a time. For example, assuming a sequence of length n, the smallest element is assumed to be at the end, then using the insert sort still needs to be moved forward and compared in one step, N-1 times and swapping.

Hill sort increases the efficiency of the insertion sort by dividing the elements to be compared into several areas. This allows elements to take a big step towards the last position at once, and then the algorithm takes smaller steps to sort. The final step is the normal insertion order of step 1, but at this point the entire sequence is already in the approximate order, so it is highly efficient.

For example, when we sort the following arrays, we start with a step of 4, which is the element divided into LMPT,EHSS. ELOX,AELR several sequences, we insert a sort of these separate sequences. After sorting is complete. We decrease the step size to continue sorting. The last until the step is 1 and the step is 1 is the general insertion sort, and he guarantees that the elements will be sorted.

Hill sort increment decrement algorithm can be arbitrarily specified, can be reduced by N/2, just to ensure that the last step is 1.

Realize:

// <summary>///Hill Sort/// </summary>///<param name= "Array" ></param>Public static voidShellsort (t[] array) {intn = array.    Length; inth = 1; //Initial maximum step size while(H < N/3) H = h * 3 + 1;  while(H >= 1) {//start with the second element for(inti = 1; i < n; i++) {            //From the beginning of the first element, sequentially and in the preceding sequence of i-h elements, assuming less than, then the Exchange for(intJ = i; j >= h; j = j-h) {                if(Array[j].                CompareTo (Array[j-h]) < 0) {Swap (array, J, j-h); }                Else//assume greater than. It is not necessary to continue to compare, because the front elements have been ordered, the larger the larger is the teaching of the big.

Break ; }} //step in addition to 3 decrement h = h/3; }}

It can be seen that the implementation of the hill sort is improved on the basis of the insertion sort, with a step of 1 for the insertion sort and a decrement of 1 for each time. The step of the hill sort is defined by our H. Then each time is compared with the element in the front-h position.

In the algorithm, we first get the maximum step size smaller than N/3, and then gradually decrement to a general insertion sort of step 1.

The following are the sort animations for the hill sort in various situations:

Analysis:

1. The key to the hill sort is the determination of the step descending sequence, which is capable of descending to a sequence of 1 steps, and now the known better sequence is:

    • Shell's sequence: N/2, N/4, ..., 1 (repeatedly divided by 2);
    • Hibbard ' s sequence: 1, 3, 7, ..., 2k-1;
    • Knuth ' s sequence: 1, 4,, ..., (3k-1)/2; the sequence is the sequence used in this article's code.

    • The best known sequence is the sequence of Sedgewick ' s (Knuth's students, Algorithems's authors): 1, 5, 19, 41, 109, ....

The sequence is obtained by interacting with the following two expressions:

    • 1, 109, 505, 2161,....., 9 (4k–2k) + 1, k = 0, 1, 2, 3,...
    • 5, 209, 929, 3905,..... 2k+2 (2k+2–3) + 1, k = 0, 1, 2, 3, ...

"The comparison is the most basic operation in the hill sort. Instead of swapping.

"Hill sorting with such steps is faster than inserting sort and heap sorting, even faster than high-speed sorting in decimal groups, but Hill sorting is slower than high-speed sorting when large amounts of data are involved."

2. The analysis of hill sequencing is more complex. The time complexity of using Hibbard's decrement step sequence is O (N3/2), the average time complexity is about O (N5/4), and the detailed complexity is still controversial at the moment.

3. Experiments show that for medium-sized sequences (million). The time complexity of hill sequencing is close to the fastest sorting algorithm of time complexity Nlogn.

Four Summary

Finally, summarize the best worst-case and average-time complexity of the three sorting algorithms presented in this article.

Name

Best

Average

Worst

Memory consumption

Stable sorting

Insert Sort

N

N2

N2

1

Is

Select sort

N2

N2

N2

1

Whether

Hill sort

N

nlog2n
Or
N3/2

dependent on increment descending sequence The best thing is nlog2n now.

1

Whether

I hope this article will help you understand the above three major sorting algorithms, and then we'll look at merge sorting and high-speed sorting later.

Super-specific reading basic sorting algorithm (no remorse, with sort demo animation)

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.