1.插入排序,最简单的排序,理想情况为N,一般情况为N的平方。
c++:
template <typename Comparable>
void insertionSort(vector<Comparable> & a)
{
int j;
for (int p=1;p<a.size();p++){
Comparable temp = a[p];
for(j=p;j>0 && temp<a[j-1];j--)
a[j] = a[j-1];
a[j] = temp;
}
}
python:
def func(a):
for i in xrange(len(a)):
temp = a[i]
j = i
while (j>0 and temp<a[j-1]):
a[j] = a[j-1]
j--
a[j] = temp
return a
原文:http://www.cnblogs.com/huangxiaohen/p/4275932.html