Hill sorting of basic algorithm series

Hill sorting, like the direct insertion above, is a type of insertion sorting. The disadvantage of the previous direct insertion sort is that if the decimal is in the lower position, the previous number must be premised, and the Hill sort takes the step size first, which can reduce the number of moves.
Hill's criterion for sorting is "take the step size first, then divide into groups; take the group as the criterion, and sort within the group". The basic code is as follows:

public static void shellSort(int[]arr){
	for(int d=arr.length/2;d>0;d/=2){		//遍历所有步长
		for(int i=d;i<arr.length;i++){		//遍历所有元素
			for(int j=i-d;j>=0;j-=d){	//遍历本组元素
				if(arr[j]>arr[j+d]){
					if(arr[j]>arr[j+d]){
						int temp=arr[j];
						arr[j]=arr[j+d];
						arr[j+d]=temp;
					}
				}
			}
		}
	}
}

The advantage is that the decimal on the right and the large on the left can be quickly moved to the corresponding position.

Guess you like

Origin blog.csdn.net/langxiaolin/article/details/112107293