编程语言
首页 > 编程语言> > 希尔排序算法

希尔排序算法

作者:互联网

@Test
	public void test013() throws Exception {
		//希尔排序,在插入排序算法的基础上做分组排序(先按一定步长排序)
		int k = 0;
		int[] arr = {4, 3, 5, 3, 6, 2, 8, 1, 9,3,5,1,2,3,6,8,0,8,7,6,5,4};
		int grap = arr.length / 2;
		while (grap > 0) {
			for (int j = grap; j < arr.length; j++) {
				for (int i = j; i >= grap; i--) {
					if (arr[i] < arr[i - grap]) {
						k++;
						int temp = arr[i];
						arr[i] = arr[i - grap];
						arr[i - grap] = temp;
					} else {
						break;
					}
				}
			}
			grap=grap/2;
		}
		System.out.println(Arrays.toString(arr));
		System.out.println("希尔排序算法次数:"+k);
	}

标签:arr,grap,算法,int,希尔,排序
来源: https://blog.csdn.net/Sandul/article/details/120457733