希尔排序百万级数据比较,循环次数少反而性能更差?求解!!!

//(1)for (int i = gap; i < arr.length; i++) { 
//(2)for (int i = gap; i < arr.length; i+=gap) {
//  ???:这里i++和i+=gap都可以,并且i+=gap循环次数更少,为啥用了反而性能更差呢

/*
* * @desc 希尔排序 * 它是一种更高效的插入排序,也称为缩小增量排序。 * 产生原因: * 由于插入排序存在问题,当需要插入数最小时,后移的次数明显增多,对效率有影响 * 基本思想: * 把记录按下标的一定增量分组,对每组使用直接插入排序算法排序; * 随着增量减少,每组包含的关键词越来越多,当增量减至1时,整个文件恰被分成一组,算法便终止 * * 案例: * {8, 9, 1, 7, 2, 3, 5, 4, 6, 0} * @Author xw * @Date 2019/9/5 */ public class ShellSort { public static void main(String[] args) { /*int[] arr = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0}; shellSort2(arr); System.out.println("第1轮过后,arr=" + Arrays.toString(arr));*/ int[] arr = new int[8000000]; for (int i = 0; i < 8000000; i++) { arr[i] = new Random().nextInt(8000000); } System.out.println(LocalDateTime.now()); shellSort2(arr); System.out.println(LocalDateTime.now()); //System.out.println("arr=" + Arrays.toString(arr)); } /** * 移动法(推荐) * @param arr */ private static void shellSort2(int[] arr) { int j; int temp; for (int gap = arr.length / 2; gap > 0; gap /= 2) { // 第1轮 [8,3] [9,5] [1,4] [7,6][2,0] for (int i = gap; i < arr.length; i++) { // ???:这里i++和i+=gap都可以,并且i+=gap循环次数更少,为啥用了反而性能更差呢 j = i; temp = arr[j]; while (j - gap >= 0 && temp < arr[ j - gap]) { // 还没找到 // 后移 arr[j] = arr[j - gap]; j -= gap; } arr[j] = temp; } } } }

 代码:https://github.com/line007/jucdemo2/blob/master/src/main/java/com/line/datastructs/sort/ShellSort.java#L46-47

gitee地址:https://gitee.com/linestyle007/jucdemo2

博客地址:https://linestyle007.gitee.io/

github地址:https://github.com/line007/jucdemo2

原文地址:https://www.cnblogs.com/ice-line/p/11474978.html