快速排序算法(牢记)

快排:// 修改自《C programing language》

#include<iostream>
#include<iterator>
#include<algorithm>

void qsort(int v[], int left, int right)
{
	int i,last;
	if(left >= right)
		return;
	std::swap(v[left],v[(left+right)/2]);
	last = left;
	for(i = left+1; i <= right; ++i)
	{
		if(v[i] < v[left])
			std::swap(v[i],v[++last]);
	}
	std::swap(v[left],v[last]);
	qsort(v,left,last-1);
	qsort(v,last+1,right);
}

int main()
{
	int a[]={5,3,1,6,8,9,4};
	qsort(a,0,7-1);
	std::copy(a,a+7,std::ostream_iterator<int>(std::cout," "));
	
	return 0;
}


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