您的位置:首页 > 其它

插入排序

2006-10-12 17:49 141 查看

// 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;


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: