Java implements Hill sorting (exchange method, shift method) diagram + code derivation

Illustration:
Insert picture description here
Hill sort code derivation:

import java.util.Arrays;

public class shellSort {
    
    
	public static void main(String[] args) {
    
    
		int[] arr = {
    
     8, 9, 1, 7, 2, 3, 5, 4, 6, 0 };
		shell(arr);
	}

	// 逐步推导
	public static void shell(int[] arr) {
    
    
		int temp = 0;
		// 希尔排序的第一轮
		// 因为第一轮排序,是将10数组分成了5组
		for (int i = 5; i < arr.length; i++) {
    
    
			// 遍历各组中的所有元素(共5组,每组2个元素),步长5
			for (int j = i - 5; j >= 0; j -= 5) {
    
    
				// 如果当前元素大于加上步长后面的那个元素,说明交换
				if (arr[j] > arr[j + 5]) {
    
    
					temp = arr[j];
					arr[j] = arr[j + 5];
					arr[j + 5] = temp;
				}
			}
		}
		System.out.println("希尔排序第一轮后:" + Arrays.toString(arr));

		
		// 希尔排序的第二轮
		// 因为第二轮排序,是将5/2=2组
		for (int i = 2; i < arr.length; i++) {
    
    
			// 遍历各组中的所有元素(共5/2),步长5
			for (int j = i - 2; j >= 0; j -= 2) {
    
    
				// 如果当前元素大于加上步长后面的那个元素,说明交换
				if (arr[j] > arr[j + 2]) {
    
    
					temp = arr[j];
					arr[j] = arr[j + 2];
					arr[j + 2] = temp;
				}
			}
		}
		System.out.println("希尔排序第二轮后:" + Arrays.toString(arr));
		
		
		// 希尔排序的第三轮
				// 因为第三轮排序,直接一组
				for (int i = 1; i < arr.length; i++) {
    
    
					// 遍历所有元素
					for (int j = i - 1; j >= 0; j -= 1) {
    
    
						// 如果当前元素大于加上步长后面的那个元素,说明交换
						if (arr[j] > arr[j + 1]) {
    
    
							temp = arr[j];
							arr[j] = arr[j + 1];
							arr[j + 1] = temp;
						}
					}
				}
				System.out.println("希尔排序第三轮后:" + Arrays.toString(arr));
	}
}

Insert picture description here
Hill sort exchange method code:

import java.util.Arrays;

import day05.forTest;

public class shell {
    
    
	public static void main(String[] args) {
    
    
		int[] arr = {
    
     8, 9, 1, 7, 2, 3, 5, 4, 6, 0 };
		shellsort(arr);
	}

	public static void shellsort(int[] arr) {
    
    

		int temp = 0;
		int count = 0;
		//gap:步长
		for (int gap = arr.length / 2; gap > 0; gap /= 2) {
    
    
			for (int i = gap; i < arr.length; i++) {
    
    
				// 遍历各组中的所有元素(共gap组,每组?个元素),步长gap
				for (int j = i - gap; j >= 0; j -= gap) {
    
    
					// 如果当前元素大于加上步长后面的那个元素,说明交换
					if (arr[j] > arr[j + gap]) {
    
    
						temp = arr[j];
						arr[j] = arr[j + gap];
						arr[j + gap] = temp;
					}
				}
			}
			System.out.println("希尔排序第"+(++count)+"后:" + Arrays.toString(arr));
		}
	}
}

Hill sort shift method:

import java.util.Arrays;

public class shell02 {
    
    
	public static void main(String[] args) {
    
    
		int[] arr = {
    
     8, 9, 1, 7, 2, 3, 5, 4, 6, 0 };
		shellsort(arr);
	}

//希尔排序,移位法
	public static void shellsort(int[] arr) {
    
    
		// 增量gap,并逐渐缩小增量
		int count =0;
		for (int gap = arr.length / 2; gap > 0; gap /= 2) {
    
    
			// 从第gap个元素,逐个对其所在的组进行直接插入排序
			for (int i = gap; i < arr.length; i++) {
    
    
				int j = i;
				int temp = arr[j];
				if (arr[j] < arr[j - gap]) {
    
    
					while (j - gap >= 0 && temp < arr[j - gap]) {
    
    
						// 移动
						arr[j] = arr[j - gap];
						j -= gap;
					}
					// 当退出while后,就给temp找到插入位置
					arr[j] = temp;
				}
			}
		}
		System.out.println("希尔排序第"+(++count)+"后:" + Arrays.toString(arr));
	}
}

Guess you like

Origin blog.csdn.net/weixin_46457946/article/details/113208731