cpp-interview/Algorithm/InsertSort.h

33 lines
890 B
C
Raw Normal View History

/*
1.
2.
3.
4. 3
5.
6. 2~5
*/
// 插入排序
void InsertSort(vector<int>& v)
{
int len = v.size();
for (int i = 1; i < len; ++i) {
int temp = v[i];
for(int j = i - 1; j >= 0; --j)
{
if(v[j] > temp)
{
v[j + 1] = v[j];
v[j] = temp;
}
else
break;
}
}
}