Diagram of the principle of nine sorting algorithms

0. Stability and complexity

Before starting, let me briefly introduce several important indicators of the sorting algorithm. Here, I try to interpret it in a fool-like way that I understand:

(1) Stability: When there are two or more keywords that are equal in the sequence, if r1 is ahead of r2 in the sequence before sorting, then if r1 is still ahead of r2 after sorting, it is stable. (The relative position of equal elements remains unchanged after sorting)

(2) Instability: When there are two or more keywords in the sequence that are equal, if r1 is ahead of r2 in the sequence before sorting, then if r1 is behind r2 after sorting, it is unstable. (The relative position of equal elements changes after sorting)

(3) Time complexity: The time overhead of an algorithm is the most important indicator of its quality. An efficient algorithm should have fewer comparisons and fewer record moves.

(4) Space complexity: that is, the auxiliary storage space required to execute the algorithm.

Sorting Algorithm Summary Table
insert image description here

1. Direct insertion sort (insert class)

Process description: Traverse the keywords in the sequence, take a keyword to be sorted each time, scan forward one by one from the previous keyword to be sorted, if the scanned keyword is greater than the keyword to be sorted, then scan The found keyword is moved back one position. Finally, the insertion position is found, and the keywords to be sorted are inserted.

void InsertSort(int R[],int n)
{ int i,j int temp; for(i=1;i<n;++i) { temp=R[i]; // temporarily store keywords to be sorted j=i-1 in temp ; //The serial number of the previous keyword to be ranked while(j>=0&&temp<R[j]) //Start scanning from the previous keyword to be ranked, if greater than For keywords to be ranked, move back one position { R[j+1]=R[j]; –j; } R[j+1]=temp; row keyword insertion } }














Worst case: When the entire sequence is in reverse order, the condition temp<R[j] of the inner loop is always true. At this time, for each outer loop, the number of inner loops reaches the maximum value each time (that is, the inner loop bit i times), the value of outer loop i is 1~i-1, so the total number of executions is n(n-1)/2.

Best case: when the entire sequence is positive. The condition of the inner loop is always not true, so the inner loop is never executed, and the statement R[j+1]=temp is always executed. So the time complexity is O(n).

Space complexity: The auxiliary storage space required by the algorithm does not change with the size of the column to be sorted. It is a constant, so it is O(1).
insert image description here

2. Half insertion sort (insert class)

Process description: The process is the same as the direct insertion sort, except that it is different from the sequential search used in the direct insertion sort, and the half search is used here. Therefore, the half insertion sort saves a lot of time in the search process than the direct insertion sort. But the number of keyword moves is the same as that of direct insertion sort.

3. Bubble sort (exchange class)

Process description: Sorting is achieved through a series of exchange actions. First, the first keyword is compared with the second keyword, if the first keyword is larger, the two are exchanged; then the second keyword is compared with the third keyword, if the second keyword is larger, the two exchange, otherwise no exchange. Go on all the way until you know which keyword with the largest value is exchanged to the end, and one trip of bubble sorting is completed.

void BubbleSort(int R[],int n)
{ int i,j,flag; int temp; for(i=n-1;i>=1;–i) { flag=0; //flag is used to mark this Is there an exchange for (j=1;j<=i;++j) { if(R[j-1]>R[j]) { temp=R[j]; R[j-1] =R[j]; R[j]=temp; flag=1; //flag=1 means that this sorting has been exchanged } if(flag==0)//If no exchange occurs, it means that the sequence is in order and sorted end return; } }

















Worst case: The sequence is in reverse order. At this time, the condition of the inner loop if statement is always true, and the number of times the basic operation is executed is ni. The value of i is 1~n-1, so the total number of executions is (n-1+1)(n-1)/2=n(n-1)/2, so the time complexity is O(n^2 ).

Best case: the sequence is in positive order. At this time, the conditional comparison statement of the inner loop is always not established, no exchange occurs, and the inner loop is executed n-1 times, so the time complexity is O(n).

Average case: time complexity O(n^2).
insert image description here

4. Simple selection sort (selection class)

Selection sorting is to continuously select the largest (or smallest) element from the unsorted elements and put it into the sorted element set until there is only one element left in the unsorted void SelectSort(int R[],int n
)
{ int i,j,k; int temp; for(i=0;i<n;++i) { k=i; for(j=i+1;j<n;++j) //from i Pick a smallest keyword in the following sequence { if(R[k]>R[j]) k=j; // temp=R[i]; R[i]=R[k]; R[k] =temp; } } }














5. Hill sort (insert class)

First, it divides a larger data set into several groups (logically grouped), and then inserts and sorts each group separately. At this time, the amount of data used by insertion sorting is relatively small (each group), and the efficiency of insertion It can be seen from a relatively high level
insert image description hereinsert image description here
that he is a group with a subscript distance of 4 points, that is to say, the subscripts with a difference of 4 are grouped into a group. For example, in this example, a[0] and a[4] are a group, a[1] and a[5] are a group..., the difference (distance) here is called increment
insert image description here
After each group is inserted and sorted, each group becomes ordered (the whole is not necessarily ordered )
![Insert picture description here](https://img-blog.csdnimg.cn/2019101811251333.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjIxMz E3Ng==, size_16, color_FFFFFF, t_70

At this point, the entire array becomes partially ordered (the degree of order may not be very high)
insert image description here

Then reduce the increment to half of the previous increment: 2, and continue to divide the grouping. At this time, the number of elements in each group is more, but the part of the array becomes orderly, and the efficiency of insertion sorting is also higher
insert image description here

In the same way, sort each group (insertion sort) so that each group is in order

Graphical algorithm --- Hill sort

Finally, set the increment to half of the previous increment: 1, then the entire array is divided into one group. At this time, the entire array is close to order, and the insertion sorting efficiency is high.

Graphical algorithm --- Hill sort

In the same way, sort the only set of data, and the sorting is completed

6. Quick sort (exchange class)

Process description: Select a keyword in the current subsequence as the pivot (generally select the first keyword as the pivot), move the subsequence smaller than the pivot to the front of the pivot, and larger than the pivot After the current exchange is completed, a new shorter subsequence is obtained, which becomes the initial sequence of the next exchange. The final position of the pivot can be determined after a sort pass. Everything smaller than the pivot is to the left of the pivot, and everything larger than the pivot is to the right of the pivot.

void QuickSort(int R[],int high,int low)
{ int temp; int i=low,j=high; if(low<high) { temp=R[low]; while(i!=j) { while (j>i&&R[j]>=temp) --j; //Scan from right to left to find a keyword smaller than pivot temp if(i<j) { R[i]=R[j]; / /Put the keywords whose right side is smaller than the pivot temp to the left of temp ++i; //The left serial number moves one bit to the right } while(i<j&&R[i]<temp) ++i;//From left to Scan right to find a keyword greater than the pivot keyword temp if(i<j) { R[j]=R[i];//put the keyword on the left greater than the pivot temp to the right of temp – j; //Move the serial number on the right one bit to the left } } R[i]=temp; QuickSort(R,low,i-1); QuickSort(R,i+1,high); } }
























Best case: the time complexity is [formula], the closer the sequence to be sorted is to unordered, the higher the efficiency of the algorithm.

Worst case: the time complexity is [formula], the closer the sequence to be sorted is to order, the lower the efficiency of this algorithm.

Average Case: Time Complexity [Formula].

Space complexity: only the auxiliary storage of temp is used from the beginning to the end, so it is O(1).
insert image description here

7. Heap sort (selection class)

Think of the heap as a complete binary tree, the big root heap - the father is big and the child is small; the small root heap - the father and the child are big.

Process description: The entire sorting process is to continuously adjust the sequence into a heap.

Take the original sequence: 49 38 65 97 76 13 27 49 as an example, adjust it to a large root pile.

(1) Adjust 97, 97>49, no need to adjust

(2) Adjust 65, 65>13, 65>27, no need to adjust
insert image description here
(3) Adjust 38, 38<97, 38<76. Need to adjust, 38 and 97 exchange, after the exchange, 38 becomes the root node of 49, continue to exchange 38 and 49.
insert image description here
(4) Adjust 49, 49<97, 49<65, so 49 is exchanged with the larger one, 97. After the exchange, 49<76 is still not satisfied with the big root pile, so exchange 49 with 76.
insert image description here

8. 2-way merge sort

void mergeSort(int A[],int low,int high)
{ if(low<high) { int mid=(low+high)/2; mergeSort(A,low,mid); //The first half of mergeSort( A, mid+1, high);//The second half of the merge sort merge(A,low,mid,high); //Merge the two ordered sequences from low to mid and mid+1 to high in the array into an ordered sequence } }








insert image description here

9. Radix sorting

"Multi-keyword sorting", (1) the highest bit first (2) the lowest bit first. For example, the highest bit first: first arrange several subsequences according to the highest bit, and then sort each subsequence according to the second highest bit.

As shown in the figure below, the sorting process of low-order first: each bucket is equivalent to a queue, and the first-in first-out rule.
insert image description here
The final result: the highest is ordered, the keywords with the same highest bit are ordered with the second highest bit, and the keywords with the same second highest bit are ordered with the lowest bit, so the whole sequence is ordered.

Guess you like

Origin blog.csdn.net/weixin_42213176/article/details/102622140