package cm.zc;
import java.util.Arrays;
/**
* 希尔排序采用移位法实现
* @author D
*
*/
public class ShellSort {
public static void main(String[] args) {
int [] arr= {8,9,1,7,2,3,5,4,6,0};
shellSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void shellSort(int[] arr) {
for(int gap=arr.length/2;gap>0;gap/=2) {
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;
}
}
}
}
JAVA--希尔排序采用移位法实现
最新推荐文章于 2024-07-30 20:50:32 发布