package com.sort;
public class ShellSort {
static void shellsort(int a[], int n)
{
int i, j, gap;
for (gap = n / 2; gap > 0; gap /= 2) //步长
for (i = 0; i < gap; i++){ //直接插入排序
for (j = i + gap; j < n; j += gap) {
int temp = a[j];
int k = j - gap;
while (k >= 0 && a[k] > temp)
{
a[k + gap] = a[k];
k -= gap;
}
a[k + gap] = temp;
}
}
}
public static void main(String[] args) {
int[] a = new int[] { 4, 3, -6, 5, 4, 3, 2, 1 };
shellsort(a, a.length);
for(int array:a){
System.out.println(array);
}
}
}
希尔排序
最新推荐文章于 2023-11-28 16:53:17 发布