学习一下希尔排序

public static void main(String[] args) {
		int[] arr = { 3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50, 48 };
		for (int gap = arr.length / 2; gap > 0; gap /= 2) {
			// 对数组元素进行分组
			for (int i = gap; i < arr.length; i++) {
				// 遍历各组中的元素
				for (int j = i - gap; j >= 0; j -= gap) {
					// 交换元素
					if (arr[j] > arr[j + gap]) {
						int temp = arr[j];
						arr[j] = arr[j + gap];
						arr[j + gap] = temp;
					}
				}
			}
		}
		for (int j = 0; j < arr.length; j++) {
			System.out.println(arr[j]);
		}
	}
发布了49 篇原创文章 · 获赞 16 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_26929957/article/details/100018764
今日推荐