插入排序实现

#include<iostream>
using namespace std;
void insert_sort(int a[],int n)
{
	for (int i = 1; i < n; i++)
	{
		int p = a[i];
		for (int j = i - 1; j >= 0; j--)
		{
			if (a[j] < p)
			{
				a[j + 1] = p;
				break;
			}
			else if (a[j] >= p)
			{
				a[j + 1] = a[j];
			}
		}
	}
}
int main()
{
	int a[10] = { 1,4,6,3,2,7,56,88,2,4};
	insert_sort(a,10);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/cx1165597739/article/details/81140538