快速排序

#include <iostream>
using namespace std;

int partition(int a[],int low,int high)
{
	int pivot = a[low];
	while(low < high)
	{
		while(low < high && pivot <= a[high])
			high--;
		a[low] = a[high];
		while(low < high && pivot >= a[low])
			low++;
		a[high] = a[low];
	}
	a[low] = pivot;
	return low;
}

void qsort(int a[],int low,int high)
{
	int pivot;
	if (low < high)
	{
		pivot = partition(a,low,high);
		qsort(a,low,pivot-1);
		qsort(a,pivot+1,high);
	}
}

int main(int argc, char const *argv[])
{
	int a[]={1,5,0,2,-11,34,11,8,3};
	qsort(a,0,sizeof(a)/sizeof(int)-1);
	
	for (int i = 0; i < sizeof(a)/sizeof(int); ++i)
		cout << a[i] << " ";
	
	return 0;
}

你可能感兴趣的:(算法,快速排序)