排序—希尔排序

来源:互联网 发布:kinect for windows 编辑:程序博客网 时间:2024/05/23 21:56

简单地说希尔排序,希尔排序使用了一个序列,h1,h2,h3...ht,叫做增量排序,在使用hk增量进行排序后,对于每个元素i,他们相隔hk的元素都是有序的

举个例子:


                                       81  94   11   96   12  35  17 95  28  58  41  75 15


在hk = 5排序后:        35  17   11   28   12  41  75 15  96  58  81 94  95


在hk = 3排序后:         28  12   11   35   15  41  58 17  94 75  81 96  95


在hk =1排序后:          11  12   15   17   28  35  41 58  75 81  94 95  96

#include <iostream>using namespace::std;void ShellSort(int *A, int n) {int i,j,Increment;for (Increment = n/2; Increment> 0; Increment = Increment/2) //<a increase Arry{for (i = Increment; i < n; i++) //<like the insertion algthory,从增量开始每个元素和对应间隔的前面的元素进行排序{int tempValue = A[i];for (j = i; j >= Increment; j-=Increment ){if (tempValue < A[j-Increment]){A[j] = A[j-Increment];A[j-Increment] = tempValue;}}}}}void main(){int A[10] = {1,3,5,4,2,6,6,7,8,3};ShellSort(A,10);for (int i = 0; i < 10; i++){cout<<A[i]<<" ";}}


0 0