Ordinary sorting is usually considered ascending.
Stability: If the relative position of two equal data before and after sorting does not change, the algorithm is said to be stable.
First: insertion sort
Principle: The components are divided into ordered intervals and disordered intervals, each time using intervals without intervals.
The first number is compared with the ordered interval.
Time complexity: O(n*n), space complexity: O(1)
Stability: Stability
The code is as follows:
public static void insertSort0(int[] array) { for (int i = 1; i < array.length; i++) { // Ordered interval [0, i) // Disordered interval [i, array.length) int key = array[i]; int j; for (j = i - 1; j >= 0 && array[j] > key; j--) { array[j + 1] = array[j]; } array[j + 1] = key; } }
II: Hill Ranking
Principle: Grouping and arranging, taking a number from the array for each gap size, and then dividing the array into several groups, repeating this operation until gap=1;
Time complexity: O(n)~O(n*n), space complexity: O(1)
Stability: instability
The code is as follows:
public static void shellSort(int[] array){ int gap=array.length; while(true){ gap=(gap/3)+1;//or gap=gap/2 insertSortWithGap(array,gap); if(gap==1){ break; } } } private static void inserSortWithGap(int[] array,int gap){ for(int i=gap;i<array.length;i++){ int key=array[i]; int j; for(j=i-gap;j>=0&&array[j]>key;j-=gap){ array[j+gap]=array[j]; } array[j+gap]=key; } }
Three: Selection and Sorting
1. One-way Selective Sorting
Principle: Select the largest (or smallest) number from the disordered interval at a time and store it at the end (front) of the disordered interval until all the data to be sorted are sorted.
Time complexity: O(n^2), space complexity: O(1)
Stability: instability
public static void selsctSort1(int[] array){ //Choose the smallest one at a time in front of the disordered interval for(int i=0;i<array.length-1;i++){//Number of times to sort //Ordered: [0,i) //Disorder: [i, array. length] int minIndex=i; for(int j=i+1;j<array.length;j++){ if(array[j]<array[minIndex]){ minIndex=j; } } swap(array,mindex,i); } } public static void selsctSort2(int[] array){ //Choose the largest one at a time and place it behind the disordered interval. for(int i=0;i<=array.length-1;i++){ //Disorder: [0,array.length-i) //Ordered: [array.length-i,array.length) int maxIndex=0; for(int j=1;j<array.length-i;j++){ if(array[j]>array[max]){ max=j; } } int t=array[max]; array[max]=array[array.length-i-1]; array[array.length-i-1]=t; }
2. Bidirectional Sorting Comparison Principle: Select the smallest + largest element from the disordered interval every time, store it in the front and last of the disordered interval until all the data elements to be sorted are finished. Time complexity: O(n^2), space complexity: O(1) Stability: instability
The code is as follows:
public static void selectSort3(int[] array){ int low=0; int high=array.length-1; //[low,high] denotes the whole disordered interval while(low<=high){ int min=low; int high=low; for(int i=low+1;i<=max;i++){ if (array[i] < array[min]) { min = i; } if (array[i] > array[max]) { max = i; } swap(array, min, low); if (max == low) { max = min; } swap(array, max, high); } private void swap(int[] array, int i, int j) { int t = array[i]; array[i] = array[j]; array[j] = t; } }