Eight major sorts - Hill sort

Hill sort

1. Algorithm thinking and process decomposition

​ Hill sort is generated on the basis of direct insertion sort , the difference is that a grouping will be performed before direct insertion sort.

For example: there is an array int[] arr = { 4 , 22 , 45 , 13 , 51 , 2 , 11 , 69 , 26 , 0 }, the length of the array is 10, and the subscripts are 0~9.

① In the first grouping, the modulo operation of (array length/2) is performed on the subscript (doing division to take the remainder) and the results are the same as a group , that is

​ 0 against 5 = 0 , 1 against 5 = 1 , 2 against 5 = 2 , 3 against 5 = 3 , 4 against 5 = 4

, 5 against 5 = 0 , 6 against 5 = 1 , 7 against 5 = 2 , 8 against 5 = 3 , 9 against 5 = 4

In this way, the numbers with subscripts 0 and 5 are divided into one group, and the numbers with subscripts 1 and 6 are grouped...

②The array { 4 , 2 } composed of arr[0] and arr[5] is directly inserted and sorted (insert 2 into the array {4}),

The result is: {2,4}

The behavior in the whole array is: { 4 , 22 , 45 , 13 , 51 , 2 , 11 , 69 , 26 , 0 }

becomes { 2 , 22 , 45 , 13 , 51 , 4 , 11 , 69 , 26 , 0 }

In the same way:

​ { 2 , 22 , 45 , 13 , 51 , 4 , 11 , 69 , 26 , 0 }---- insert (exchange) ------>{ 2 , 11 , 45 , 13 , 51 , 4 , 22 , 69 , 26 , 0 } ---------->

------->{ 2 , 11 , 45 , 13 , 51 , 4 , 22 , 69 , 26 , 0 }—not inserted ---->

------->{ 2 , 11 , 45 , 13 , 51 , 4 , 22 , 69 , 26 , 0 }—not inserted ---->

------->{ 2 , 11 , 45 , 13 , 51 , 4 , 22 , 69 , 26 , 0 }---- insert ------>{ 2 , 11 , 45 , 13 , 0 , 4, 22, 69, 26, 51 }

③ The second grouping takes (array length/2/2), which is 2,

The grouping result is { 2 , 11 , 45 , 13 , 0 , 4 , 22 , 69 , 26 , 51 }

The inline procedure is:

First group: { 2 , 11 , 45 , 13 , 0 , 4 , 22 , 69 , 26 , 51 }---------->{ 2 , 11 , 45 , 13 , 0 , 4 , 22 , 69 , 26 , 51 }------------>

--------->{ 0 , 11 , 2 , 13 , 45 , 4 , 22 , 69 , 26 , 51 }------------>{ 0 , 11 , 2 , 13 , 22 , 4 , 45 , 69 , 26 , 51 }------------>

--------->{ 0 , 11 , 2 , 13 , 22 , 4 , 26 , 69 , 45 , 51 }

Second group: { 0 , 11 , 2 , 13 , 22 , 4 , 26 , 69 , 45 , 51 }---------->{ 0 , 4 , 2 , 11 , 22 , 13 , 26 , 69 , 45 , 51 }--------->

---------->{ 0 , 4 , 2 , 11 , 22 , 13 , 26 , 69 , 45 , 51 }----------->{ 0 , 4 , 2 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }

④ The third grouping (array length/2/2/2) is 1 , which will be the last grouping, and the array will be sorted after this simple selection sort.

The grouping result is { 0 , 4 , 2 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }

The direct insertion process is: { 0 , 4 , 2 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }---------->{ 0 , 4 , 2 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }------->

--------->{ 0 , 4 , 2 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }---------->{ 0 , 2 , 4 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }------------>

--------->{ 0 , 2 , 4 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }---------->{ 0 , 2 , 4 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }----------->

--------->{ 0 , 2 , 4 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }---------->{ 0 , 2 , 4 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }----------->

--------->{ 0 , 2 , 4 , 11 , 22 , 13 , 26 , 51 , 45 , 69 }---------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 51 , 45 , 69 }------------>

--------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 51 , 45 , 69 }---------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 51 , 45 , 69 }------------>

--------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 51 , 45 , 69 }---------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 51 , 45 , 69 }------------>

--------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 51 , 45 , 69 }---------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 45 , 51, 69 }------------>

--------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 51 , 45 , 69 }---------->{ 0 , 2 , 4 , 11 , 13, 22 , 26 , 45 , 51, 69 }------------>

At this point, Hill sorting is complete.

2. Code implementation

package sort;

public class ShellSort {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		int[] arr = {
    
     4, 22, 45, 13, 51, 2, 11, 69, 26, 0 };
		show(arr);
		arr = shellSort(arr);
		show(arr);
	}

	// 希尔排序
	public static int[] shellSort(int[] arr) {
    
    
		for (int mod = arr.length / 2; mod > 0; mod /= 2) {
    
    
			for (int i = 0; i < arr.length - 1; i += mod) {
    
    
				for (int j = i; j >= 0; j -= mod) {
    
    
					if (arr[j] > arr[j + 1]) {
    
    
						int t = arr[j];
						arr[j] = arr[j + 1];
						arr[j + 1] = t;
					} else {
    
    
						break;
					}
				}

			}
		}
		return arr;
	}

	public static void show(int[] arr) {
    
    
		for (int i : arr) {
    
    
			System.out.print(i + " ");
		}
		System.out.println();
	}
}

3. Running results

insert image description here

Guess you like

Origin blog.csdn.net/War_wick/article/details/126754969