快速排序——从大到小

快速排序——从大到小:
只需要修改两个条件就可以:
1、if(a[high] <= p && low < high)
2、if(a[low] > p && low < high)

#include
#include
using namespace std;
int partion(int low,int high,int *a);
void quick_sort(int low ,int high,int *a);
int main()
{
	int a[10001];
	memset(a,0,sizeof(a));
	int i;
	int n;
	cin >> n;
	for(i = 0;i < n ;i ++)
		cin >> a[i];
	quick_sort(0,n - 1,a);
	for(i =0;i < n - 1;i ++)
	{
		cout << a[i]<< " ";
	}
	cout << a[n - 1] << endl;
	return 0;
 } 
 
int partion(int low,int high,int *a)
{
	int p = a[low];
	
	while(low < high)
	{
		if(a[high] <= p && low < high)
		{
			high --;
		}
		a[low] = a[high];
		
		if(a[low] > p && low < high)
		{
			low ++;
		}
		
		a[high] = a[low];
	}
	a[low] = p;
	return low;
}


void quick_sort(int low ,int high,int *a)
{
	if(low < high)
	{
		int p = partion(low,high,a);
		quick_sort(low ,p - 1,a);
		quick_sort(p + 1 ,high,a);
	}
}

你可能感兴趣的:(快速排序——从大到小)