#include <iostream>
#include <cstdlib>
using namespace std;
/*
* name : insertion sort
* author : sangoly
* O(n^2)
* S(1)
* date : 2014/4/15
*/
void insert_sort(int a[], int length)
{
for (int j=1; j<length; j++) {
int key = a[j];
int i = j-1;
while (i>=0 && a[i]>key) {
a[i+1] = a[i];
i--;
}
a[i+1] = key;
}
}
int main(int argc, const char* argv[])
{
int a[6] = {31, 41, 59, 26, 41, 58};
insert_sort(a, 6);
for (int i=0; i<6; i++){
cout<<a[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}
插入排序
最新推荐文章于 2024-12-13 15:13:27 发布