算法笔记 --- Shell Sort

#include <iostream>

using namespace std;
class ShellSort {
public:
    int* shellSort(int* A, int n) {
        // write code here
        for(int gap = n / 2; gap > 0; gap /= 2){
            for(int index_beg = 0; index_beg < gap; index_beg++){
                insertionSort(A, n, gap, index_beg);
            }
        }
        return A;
    }
    void insertionSort(int* A, int n, int gap, int index_beg) {
        // write code here
        int tmp;
        int index_insert;
        for(int index = index_beg+gap; index < n; index += gap){
            index_insert = index;
            while(A[index_insert] < A[index_insert - gap] && index_insert >= gap){
                tmp = A[index_insert];
                A[index_insert] = A[index_insert - gap];
                A[index_insert - gap] = tmp;
                index_insert-=gap;
            }
        }
    }
};
int main()
{
    int a[6] = {1, 5, 7, 4, 2, 9};
    int* res;
    ShellSort sorter;
    res = sorter.shellSort(a, 6);
    cout<<"after sorting:"<<endl;
    for(int i = 0; i < 6; i++){
        cout<<a[i]<<" ";
    }
    cout<<endl;
   
    return 0;
}

 

posted @ 2016-08-20 19:39  zhongzhiqiangZz  阅读(122)  评论(0编辑  收藏  举报