快速排序

#include <iostream>
using namespace std;

void print(int a[], int n){
for(int j= 0; j<n; j++){
cout<<a[j] <<" ";
}
cout<<endl;
}

int partition(int a[], int low, int high)
{
int privotval = a[low];

while (low < high)
{
while (low < high & privotval < a[high])high--;
a[low] = a[high];
while (low < high & privotval > a[low])low++;
a[high] = a[low];
}
a[low] = privotval;
return low;
}

int quicksort(int a[], int low, int high)
{
if (low < high)
{
int privotloc = partition(a, low, high);
quicksort(a, low, privotloc-1);
quicksort(a, privotloc+1, high);
}
}

int main(){
int a[10] = {3,1,5,7,2,4,9,6,10,8};
cout<<"初始值:";
print(a,10);
quicksort(a,0,9);
cout<<"结果:";
print(a,10);
}

posted @ 2012-10-23 22:20  SA高处不胜寒  阅读(138)  评论(0编辑  收藏  举报