直接插入排序,从小到大排序
#include<stdio.h>
#include<iostream>
using namespace std;
void insertsort(int a[], int n) //核心代码
{
int i, j;
int temp;
for (i = 1; i < n; i++)
{
temp = a[i];
j = i - 1;
while (j >= 0&& temp<a[j])
{
a[j + 1] = a[j];
j--;
}
a[j + 1] = temp;
}
}
int main()
{
int i, a[10] = {2,5,6,3,7,8,0,9,12,1 };
cout << "the orginal data array is" << endl;
for (i = 0; i <10; i++)
cout << a[i] << " ";
cout << endl;
insertsort(a, 10);
cout << "the result" << endl;
for (i = 0; i <10; i++)
cout << a[i] << " ";
cout << endl;
return 0;
}