插入排序InserSort

来源:互联网 发布:c语言程序流程图软件 编辑:程序博客网 时间:2024/06/15 15:08
#include <iostream>
using namespace std;
void InsertSort(int a[], int length)
{
int n = length;
int key;
for (int j = 1; j < n; j++)
{
key = a[j];
int i = j - 1;
while (i > 0 && a[i] > key)
{
a[i + 1] = a[i];
i = i - 1;
}
a[i+1] = key;
}
}
int main()
{
int n,i;
int a[100];
cout << "Please input the number of array : ";
cin >> n;
cout << "Please input the array : ";
for (i = 0; i < n; i++)
cin >> a[i];
InsertSort(a, n);
cout << "The array after insertsort is :" << endl;
for (i = 0; i < n; i++)
cout << a[i] << " ";
system("pause");

}

0 0