快排

#include<iostream>
using namespace std;
void QuickSort(int e[], int first, int end)
{
	int i = first, j = end;
	int temp = e[first];//记录第一个数据  

	while (i<j)
	{
		while (i<j && e[j] >= temp)  //与first数据比较,右边下标逐渐左移  
			j--;

		e[i] = e[j];

		while (i<j && e[i] <= temp)  //与first数据比较,左边下标逐渐右移  
			i++;

		e[j] = e[i];
	}
	e[i] = temp;                      //将first数据放置于i=j处  

	if (first<i - 1)
		QuickSort(e, first, i - 1);
	if (end>i + 1)
		QuickSort(e, i + 1, end);
}
void main()
{
	int a[5];
	for (int i = 0; i < 5; i++)
	{
		cin >> a[i];
	}
	QuickSort(a, 0, 4);
	for (int i = 0; i < 5; i++)
	{
		cout<< a[i]<<' ';
	}
}

你可能感兴趣的:(快排)