希尔排序

来源:互联网 发布:阿桑奇曝光中国知乎 编辑:程序博客网 时间:2024/05/19 17:10
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);}}}

0 0
原创粉丝点击