希尔排序

  1. 希尔排序

希尔排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* 的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入
* @param args
*/

public static void main(String[] args) {

int arr[] ={75,70,85,80,60,100,90};
ShellSort(arr);
System.out.println(Arrays.toString(arr));
}



public static int[] ShellSort( int[] arr){
//int arr[] ={75,70,85,80,60,100,90};
// int grow= arr.length/2;
int grow= arr.length/2;
while(grow>=1){
for(int i =grow; i<arr.length ;i++){

int insertValue =arr[i];

// 插入的位置
int insertIndex=i-grow;
while( insertIndex >=0 && insertValue < arr[insertIndex]){
//待插入的值比已排序部分的元素值小,已排序部分当前比较元素后移
arr[insertIndex+grow] =arr[insertIndex];
//待插入的值比已排序部分的元素值小,插入位置前移
insertIndex= insertIndex -grow;
}
//则将待插入的值赋值到对应位置的数组中
arr[insertIndex+grow] = insertValue;
}
grow=grow/2;
}

return arr;
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 451261884@qq.com

文章标题:希尔排序

文章字数:234

本文作者:汤高

发布时间:2019-10-13, 16:49:36

最后更新:2019-10-13, 17:41:29

原始链接:https://tanggao1314.github.io/2019/10/13/希尔排序/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏