1. Insertion sort
(1), the main idea:
- Suppose the array is divided into two parts, the ordered part [0~i-1], and the unordered part [i~N]. The initial ordered part has only one element.
- Find a position whose value is less than (or greater than) the array [i] from the ordered part [0~i-1], which is the data to be sorted
- Insert the array [i] into the appropriate position, and transfer other data later
(2), code implementation:
public void sort(int[] arr) { for(int i=1;i<arr.length;i++){ int insertValue = arr[i]; int j; for(j=i-1;j>=0;j--){//1. Find a position from the arr[i-1]~arr[0] array if(insertValue > arr[j]){ break; } } //2. If j==i-1, it means that there is no need to exchange, just in the sorted position if(j==i-1){ continue; } //3. Find an exact location for(int k =i-1;k>j;k--){ arr[k+1] = arr[k]; } arr[j+1] = insertValue; } }
2. Hill sort
Hill sort is an improved algorithm for insertion sort, a grouping insertion sort, also known as shrinking incremental sorting.
The time complexity of Hill sorting is related to the choice of increment (ie, step gap). For example, when the increment is 1, Hill sort degenerates into direct insertion sort, and the time complexity at this time is O(N²), while the time complexity of Hibbard increment (N/2) Hill sort is O (N3/2).
- For an array of array length N, take an integer gap less than N (gap is called step size)
- Divide the array into several sub-arrays according to the step size, and put all records whose distance is a multiple of the gap in the same sub-array
- Insertion sort on each subarray
- Then reduce the value of the gap and repeat the above grouping and sorting
- When gap==1, the entire array is sorted
The following takes the sequence {80, 30, 60, 40, 20, 10, 50, 70} as an example to demonstrate its Hill sorting process.
1st trip: (gap=4)
When gap=4, it means to divide the sequence into 4 groups: {80,20},{30,10},{60,50},{40,70}. Corresponding sequence
: {80, 30, 60, 40, 20, 10, 50, 70} Sort these 4 groups respectively, the sorting result: {20, 80}, {10, 30}, {50, 60}, {40,70}. Corresponding sequence: {20,10,50,40,80,30,60,70}
2nd trip: (gap=2)
When gap=2, it means to divide the sequence into 2 groups: {20,50,80,60}, {10,40,30,70}. Corresponding sequence: {20,10,50,40,80,30,60,70}
Note: {20,50,80,60} actually has two ordered sequences {20,80} and {50,60 }composition.
{10,40,30,70} actually consists of two ordered sequences {10,30} and {40,70}.
Sort these 2 groups separately, and sort the results: {20,50,60,80}, {10,30,40,70}. Corresponding sequence: {20,10,50,30,60,40,80,70}
3rd trip: (gap=1)
When gap=1, it means to divide the sequence into 1 group: {20, 10, 50, 30, 60, 40, 80, 70}
Note: {20, 10, 50, 30, 60, 40, 80, 70} actually consists of two ordered sequences {20,50,60,80} and {10,30,40,70}.
Sort these 1 groups separately, and the sorting result: {10, 20, 30, 40, 50, 60, 70, 80}
(3), code implementation
public void sort(int[] arr) { // gap is the step size, which is reduced to half of the original each time. for (int gap = arr.length / 2; gap > 0; gap /= 2) { // A total of gap groups, perform direct insertion sort on each group for (int i = 0; i < gap; i++) { group_sort(arr, arr.length, i, gap); } } } private void group_sort(int arr[], int n, int i, int gap) { for (int j = i + gap; j < n; j += gap) { // If a[j] < a[j-gap], find the position of a[j] and move the position of the following data back. if (arr[j] < arr[j - gap]) { int tmp = arr[j]; int k = j - gap; while (k >= 0 && arr[k] > tmp) { arr[k + gap] = arr[k]; k -= gap; } arr[k + gap] = tmp; } } }