快速排序
#include <iostream>
using namespace std;
#define MAXSIZE 6
void QuickSort(int *a,int low,int high);
int FindPosition(int* a,int low,int high);
int main()
{
int a[MAXSIZE]={38,66,13,97,22,49};
cout<<"排序完的结果为"<<endl;
QuickSort(a,0,MAXSIZE-1);
for(int i=0;i<MAXSIZE;i++)
cout<<a[i]<<" ";
return 0;
}
void QuickSort(int *a,int low,int high)
{
int pos;
if(low<high)
{
pos=FindPosition(a,low,high);
QuickSort(a,low,pos-1);
QuickSort(a,pos+1,high);
}
}
int FindPosition(int* a,int low,int high)
{
int val=a[low];
while(high!=low)
{
while(a[high]>val&&high!=low)
--high;
a[low]=a[high];
while(a[low]<val&&high!=low)
++low;
a[high]=a[low];
}
a[low]=val;
return low;
}
结果