希尔排序

#include <iostream>
#include<algorithm>
using namespace std;

class  ShellSort{
public:
    void swap(int *A, int i, int j)
    {
        int temp = A[i];
        A[i] = A[j];
        A[j] = temp;
    }

    int* shellSort(int* A, int n) {
        for (int feet = n / 2; feet > 0; feet /= 2)
        {
            for (int i = feet; i < n; i++)
            {
                for (int j = i; j >= feet; j -= feet)
                {
                    if (A[j] < A[j - feet]) swap(A, j, j - feet);
                    else break;
                }
            }
        }
        return A;
    }
};
int main(int argc, char *argv[])
{
    int a[]={16,7,3,20,17,8};
    int size;
    ShellSort* hs = new ShellSort();
    hs->shellSort(a, 6);
    int i;
        for (i = 0; i < 6; i++)
            cout << a[i] << " ";
        cout << endl;
        system("pause");
    return 0;
}

你可能感兴趣的:(希尔排序)