简单插入排序(C++版)

#include <iostream>

using namespace std;

/** \ Insert Sort
 *
 *  Key:
 *  * reserve: tm = a[i]
 *  * position: int j = i-1
 *  * move : while
 *
 */

template <typename T>
void insertSort(T a[], int n) {
    T tm;
    for (int i = 1; i < n; i++) {
        tm = a[i];
        int j = i-1;
        while (a[j] > tm && j >= 0){
            a[j+1] = a[j];
            j--;
        }
        a[j+1]=tm;
    }
}

 

posted on 2016-02-21 21:49  qmzp  阅读(264)  评论(0编辑  收藏  举报

导航