Sorting algorithm | Average time complexity |
---|---|

Bubble sort | O(n^2) |

Selection sort | O(n^2) |

Insertion sort | O(n^2) |

Shell Sort | O(n^1.5) |

Quick sort | O(N*logN) |

Merge sort | O(N*logN) |

Heap sort | O(N*logN) |

Radix sorting | O(d(n+r)) |

① Bubble sort

Basic ideas:

The two numbers are relatively large. The larger number sinks and the smaller number rises.

Process:

Compare the two adjacent data. If the second number is small, exchange the location.

Compare from the back to the front, all the way to the first two data. Finally, the decimal point is swapped to the starting position, so that the first decimal point is arranged.

Continue to repeat the above process, and then add 2.3 n-1 minimum number of arranged positions.

Average time complexity:

O(n^2)

java code implementation:

public static void BubbleSort(int [] arr){ int temp;//Temporary variable for(int i=0; i<arr.length-1; i++){ //Represents the number of trips, a total of arr.length-1. for(int j=arr.length-1; j>i; j--){ if(arr[j] < arr[j-1]){ temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; } } } }

Optimization:

To solve the problem: after the data is arranged in order, the bubbling algorithm will continue to carry out the next round of comparison until arr.length-1, and the latter comparison is meaningless.

The plan:

Set flag bit flag to true if exchange occurs, and false if no exchange occurs.

In this way, if the flag is still false after a round of comparison, i.e. there is no exchange in this round, it means that the data sequence has been arranged and there is no need to continue.

public static void BubbleSort1(int [] arr){ int temp;//Temporary variable boolean flag;//Flag whether to exchange for(int i=0; i<arr.length-1; i++){ //Represents the number of times, a total of arr.length-1 times // Every time the flag bit is traversed, it must be set to false to determine whether the following elements have been exchanged flag = false; for(int j=arr.length-1; j>i; j--){ //Select the maximum value of the sequence to move backward if(arr[j] < arr[j-1]){ temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; flag = true; //The flag is set to true as long as there is an exchange } } // Determine whether the flag bit is false. If it is false, it means that the following elements are in order. return directly if(!flag) break; } }

② Selection sort

Basic ideas:

In the length of N-disordered array, the number of n-1 is traversed for the first time, and the smallest value is found to exchange with the first element;

In the second time, the number of n-2 is traversed to find the smallest value to exchange with the second element;

. . .

The n-1 traversal finds the smallest value and exchanges with the n-1 element, and the sorting is completed.

Process:

Average time complexity:

O(n^2)

java code implementation:

public static void select_sort(int array[],int lenth){ for(int i=0;i<lenth-1;i++){ int minIndex = i; for(int j=i+1;j<lenth;j++){ if(array[j]<array[minIndex]){ minIndex = j; } } if(minIndex != i){ int temp = array[i]; array[i] = array[minIndex]; array[minIndex] = temp; } } }

③ Insertion sort

Basic ideas:

In a set of numbers to be sorted, suppose that the first n-1 numbers have been ordered, now insert the nth number into the previous ordered number series, so that the nth numbers are also ordered. Repeat the cycle until all are in order.

Process:

Average time complexity:

O(n^2)

java code implementation:

public static void insert_sort(int array[],int lenth){ int temp; for(int i=0;i<lenth-1;i++){ for(int j=i+1;j>0;j--){ if(array[j] < array[j-1]){ temp = array[j-1]; array[j-1] = array[j]; array[j] = temp; }else{ //No exchange required break; } } }

④ Shell sort

Be careful:

If the data sequence is basically in order, insert sorting is more efficient.

Basic ideas:

In a group of numbers to be sorted, they are divided into several subsequences according to a certain increment, and the subsequences are inserted and sorted respectively.

Then gradually reduce the increment and repeat the above process. Until the increment is 1, at this time, the data sequence is basically in order, and finally the insertion sorting is performed.

Process:

Average time complexity:

O(n^1.5)

java code implementation:

public static void shell_sort(int array[],int lenth){ int temp = 0; int incre = lenth; while(true){ incre = incre/2; for(int k = 0;k<incre;k++){ //Divide into several subsequences according to the increment for(int i=k+incre;i<lenth;i+=incre){ for(int j=i;j>k;j-=incre){ if(array[j]<array[j-incre]){ temp = array[j-incre]; array[j-incre] = array[j]; array[j] = temp; }else{ break; } } } } if(incre == 1){ break; } } }

⑤ Quicksort

Basic idea: (divide and rule)

First, take a number from the sequence as the key value;

Put all numbers smaller than this number on its left, and all numbers greater than or equal to it on its right;

Repeat the second step for the left and right small sequences until there is only one number in each interval.

Auxiliary understanding: number of excavation and filling

Initially, I = 0; J = 9; key = 72

Since the number in a[0] has been saved in the key, it can be understood that a hole has been dug in the array a[0], and other data can be filled here.

Start with j and find a number smaller than key. When j=8, a[0] = a[8]; I + +; excavate a[8] and fill it into the previous pit a[0].

So a hole a[0] is solved, but a new hole a[8] is formed. What's to be done? Simply, find a number to fill in the hole a[8].

This time, start from i and find a number greater than key. When i=3, it meets the conditions, a [8] = a[3]; J --; dig out a[3] and fill it in the previous pit.

array | 72 | - 6 | - 57 | - 88 | - 60 | - 42 | - 83 | - 73 | - 48 | - 85 |
---|---|---|---|---|---|---|---|---|---|---|

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

Now I = 3; J = 7; key = 72

Repeat the above steps, first from the back to the front, then from the front to the back.

Starting from j, when j=5, meet the conditions, excavate and fill a[5] into the previous pit, a[3] = a[5]; i + +;

Find backward from i, when i=5, exit due to i==j.

At this time, i = j = 5, and a[5] happens to be the last pit dug, so fill the key in a[5].

array | 48 | - 6 | - 57 | - 88 | - 60 | - 42 | - 83 | - 73 | - 88 | - 85 |
---|---|---|---|---|---|---|---|---|---|---|

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

It can be seen that the numbers in front of a[5] are smaller than it, and the numbers behind a[5] are larger than it. So for a[0 4] and a[6... 9] Repeat the above steps for these two sub intervals.

array | 48 | - 6 | - 57 | -42 | - 60 | - 72 | - 83 | - 73 | - 88 | - 85 |
---|---|---|---|---|---|---|---|---|---|---|

0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

Average time complexity:

O(N*logN)

Code implementation:

public static void quickSort(int a[],int l,int r){ if(l>=r) return; int i = l; int j = r; int key = a[l];//Select the first number as key while(i<j){ while(i<j && a[j]>=key)//Find the first value less than key from right to left j--; if(i<j){ a[i] = a[j]; i++; } while(i<j && a[i]<key)//Find the first value greater than key from left to right i++; if(i<j){ a[j] = a[i]; j--; } } //i == j a[i] = key; quickSort(a, l, i-1);//Recursive call quickSort(a, i+1, r);//Recursive call }

Be careful:

The selection of key value can take many forms, such as middle number or random number, which will have different effects on the complexity of the algorithm.

⑥ Merge sort

Basic ideas:

Merge sorting is an effective sorting algorithm based on merge operation. This algorithm is a typical application of divide and conquer method.

First of all, consider how to combine two ordered sequences. This is very simple. As long as you compare the first number of two sequences, you can take the first one when it is small, and then delete the number in the corresponding sequence. Then compare, if there is a sequence of empty, then directly take out the data of another sequence in turn.

//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 && j < m){ if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } while (i < n) c[k++] = a[i++]; while (j < m) c[k++] = b[j++]; }

To solve the above problem of merging ordered sequence, let's look at merging and sorting again. Its basic idea is to divide the array into two groups A and B. if the data in these two groups are all orderly, then it is very convenient to sort these two groups of data. How to make the data in these two groups orderly?

Group A and B can be divided into two groups. By analogy, when there is only one data in the divided group, it can be considered that the order has been achieved in this group, and then the two adjacent groups can be merged. In this way, the merging and sorting are completed by decomposing the sequence recursively and then merging the sequence.

Process:

Average time complexity:

O(NlogN)

The efficiency of merging and sorting is relatively high. It takes logN steps to set the length of the sequence as N, and divide the sequence into small sequence. Each step is a process of merging the sequence. The time complexity can be recorded as O(N), so the total is O(NlogN).

Code implementation:

public static void merge_sort(int a[],int first,int last,int temp[]){ if(first < last){ int middle = (first + last)/2; merge_sort(a,first,middle,temp);//Left half in order merge_sort(a,middle+1,last,temp);//Right half in order mergeArray(a,first,middle,last,temp); //Merge left and right } }

//Merge: merge two sequences a [first middle], a [middle + 1-end] public static void mergeArray(int a[],int first,int middle,int end,int temp[]){ int i = first; int m = middle; int j = middle+1; int n = end; int k = 0; while(i<=m && j<=n){ if(a[i] <= a[j]){ temp[k] = a[i]; k++; i++; }else{ temp[k] = a[j]; k++; j++; } } while(i<=m){ temp[k] = a[i]; k++; i++; } while(j<=n){ temp[k] = a[j]; k++; j++; } for(int ii=0;ii<k;ii++){ a[first + ii] = temp[ii]; } }

⑦ Heap sort

Basic ideas:

Average time complexity:

O(N*logN)

Because the time complexity of each time to restore the heap is O(logN), a total of N - 1 times to restore the heap operation, plus N / 2 times of downward adjustment when the heap was previously established, the time complexity of each adjustment is also O(logN). Add the second operation time or O(N * logN).

java code implementation:

//Build minimum heap public static void MakeMinHeap(int a[], int n){ for(int i=(n-1)/2 ; i>=0 ; i--){ MinHeapFixdown(a,i,n); } } //Adjust from node i, n is the total number of nodes. The number of child nodes of node I is 2*i+1, 2*i+2 public static void MinHeapFixdown(int a[],int i,int n){ int j = 2*i+1; //Child node int temp = 0; while(j<n){ //Find the smallest among the left and right child nodes if(j+1<n && a[j+1]<a[j]){ j++; } if(a[i] <= a[j]) break; //Large node down temp = a[i]; a[i] = a[j]; a[j] = temp; i = j; j = 2*i+1; } }

public static void MinHeap_Sort(int a[],int n){ int temp = 0; MakeMinHeap(a,n); for(int i=n-1;i>0;i--){ temp = a[0]; a[0] = a[i]; a[i] = temp; MinHeapFixdown(a,0,i); } }

⑧ Radix sort

Basic ideas:

Binport's idea is very simple. First, create array A[MaxValue]; then put each number in the corresponding position (for example, 17 in the array position of subscript 17); finally, traverse the array, which is the result after sorting.

Question:

When there is a large value in the sequence, BinSort will waste a lot of space.

Solve:

Basic ideas:

Cardinality sorting is based on binport, which reduces the space overhead through cardinality restriction.

(1)

(2)

(1) First, the cardinality is determined to be 10, and the length of the array is 10. Each number 34 will find its own position in the 10 numbers.

(2) Unlike binport, which directly places the number 34 at the index 34 of the array, cardinality sorting divides 34 into 3 and 4. The first round of sorting is placed at the index 4 of the array according to the last bit, the second round is placed at the index 3 of the array according to the last bit, and then the array can be traversed.

java code implementation:

public static void RadixSort(int A[],int temp[],int n,int k,int r,int cnt[]){ //A: original array //temp: temporary array //n: Number of digits in the sequence //k: Maximum number of digits 2 //r: base 10 //cnt: number of bin[i] stored for(int i=0 , rtok=1; i<k ; i++ ,rtok = rtok*r){ //Initialization for(int j=0;j<r;j++){ cnt[j] = 0; } //Count the number of each box for(int j=0;j<n;j++){ cnt[(A[j]/rtok)%r]++; } //The number of cnt[j] is changed to the number of the first j boxes for(int j=1;j<r;j++){ cnt[j] = cnt[j-1] + cnt[j]; } for(int j = n-1;j>=0;j--){ //Key understanding cnt[(A[j]/rtok)%r]--; temp[cnt[(A[j]/rtok)%r]] = A[j]; } for(int j=0;j<n;j++){ A[j] = temp[j]; } } }