快速排序代码

#include <iostream>
using namespace std;

void swap(int* p, int* q)
{
	int temp= *p;
	*p =*q;
	*q =temp;
}
int partition(int* a,int low,int high)
{
	int pivot =a[low];
	while(low<high)
	{
		while((pivot<=a[high])&&(high>low))  high--;
		swap(&a[low],&a[high]);
		while((pivot>=a[low])&&(high>low))   low++;
		
		swap(&a[low],&a[high]);
	}
	return low;
}

int qsort(int* a,int low,int high)
{
	if(low<high)
	{
		int key = partition(a,low,high);
		qsort(a,low,key-1);
		qsort(a,key+1,high);
	}
}

int main()
{
	int a[]={3,7,2,5,1,6,0,4,5,7};
	for(int i=0;i<10;i++)
	{
		cout<<a[i]<<endl;
	}
	qsort(a,0,9);
	cout<<"hello"<<endl; 
	for(int i=0;i<10;i++)
	{
		cout<<a[i]<<endl;
	}
}

  

posted on 2013-03-19 16:05  mymemory  阅读(122)  评论(0编辑  收藏  举报

导航