希尔排序(C++版)

#include <iostream>

using namespace std;

/** Shell Sort 
 *
 * Key:
 * * increment
 * * insertSort(:increment)
 *
 */     

template <typename T>
void insertSort_shell(T* a, int n, int incre) {
    T tm;
    for(int i = incre; i < n; i = i+incre) {  /// how many to
        tm = a[i];
        int j = i - incre;
        while(a[j] > tm && j >=0){
            a[j+incre] = a[j];
            j = j - incre;
        }
        a[j+incre] = tm;
    }
}

template <typename T>
void shellSort(T* a, int n) {
    int incre = n/2;
    while (incre >=1) {
        for(int i = 0; i < incre; i++) {
            insertSort_shell(a, n, incre);
        }
        incre = incre / 2;
    }
}

 

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