首页 > 代码库 > 希尔排序

希尔排序

比较相距一定间隔的元素,间隔不断缩小

for (increment=N/2; increment>0; increment/=2) {
    for (i=increment; i<N; i++) {
        tmp = A[i];
        for (j=i; j>=increment; j-=increment) {
            if (tmp < A[j-increment]) {
                A[j] = A[j-increment];
            }
            else
                break;
        }
        A[j] = tmp;
    }
}

 

希尔排序