#include<iostream>
using namespace std;
void insertsort(int a[], int n)
{
for (int j = 1; j < n; j++) {
for (int i = 0; i < j; i++)
{
if (a[j] < a[i]) {
int t = a[j];
for (int k = j - 1; k >= i; k--)
a[k + 1] = a[k];
a[i] = t;
break;
}
}
}
}
int main()
{
int a[] = { 5,4,3,2,1 };
insertsort(a, 5);
for (int i = 0; i < 5; i++)
cout << a[i] << " ";
return 0;
}
插入排序法
最新推荐文章于 2024-08-29 16:37:54 发布