cpp-interview/Algorithm/InsertSort.h

32 lines
755 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 - 1; ++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;
}
}
}