#include <iostream>
#include <iomanip>
using namespace std;
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void insertion(int a[], int sz)
{
for(int i=1; i < sz; i++)
{
int j = i;
while(j > 0 && (a[j] < a[j-1]))
{
swap(a[j], a[j-1]);
j--;
}
for (int k = 0; k < sz; k++) cout << setw(3) << a[k];
cout<<endl;
}
}
int main()
{
int a[] = { 15, 9, 8, 1, 4, 11, 7, 12, 13, 6, 5, 3, 16, 2, 10, 14};
int size = sizeof(a)/sizeof(int);
for (int i = 0; i < size; i++) cout << setw(3) << a[i];
cout<<endl;
insertion(a, size);
cout << endl;
return 0;
}
直接插入排序
最新推荐文章于 2024-03-19 18:50:33 发布