// insert.cpp : Defines the entry point for the console application. // 当只有一个元素时,元素是有序的。然后插入,比较大小,大者在后。 #include " stdafx.h " // 第一种方法: template < class T > void Insert(T a[], int n, const T & x) ... { for(int i = n-1; i >= 0 && x < a[i]; i--) a[i+1] = a[i]; a[i+1] = x;} template < class T > void InsertionSort(T a[], int n) ... { for(int i = 1; i < n; i++) ...{ T t= a[i]; Insert(a, i, t); }} // 第二种方法: template < class T > void InsertionSort(T a[], int n) ... { for(int i = 1; i < n; i++) ...{ T t = a[i]; for(j = i -1; j > 0 && t < a[j]; j--) a[j+1] = a[j]; a[j+1] = t; }} int main( int argc, char * argv[]) ... { return 0;}