InsertSort

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
//插入排序-升序
void InsertSort(int a[], int n)
{
	for (int j = 1; j < n; 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(void)
{
	int a[] = { 4,3,2,1,0 };
	InsertSort(a, 5);
	for (int i = 0; i < 5; i++)
		cout << a[i] << endl;
	return 0;
}
posted @ 2019-10-23 21:28  ManateeFan  阅读(103)  评论(0编辑  收藏  举报