重点算法--快速排序

#include<iostream>
using namespace std;
////写出快速排序,归并排序与堆排序


int adjustarray(int a[],int left,int right)
{
    int x = a[left];
    while(left < right)
    {
        while(a[right] > x && left < right) right--; ///注意这个小于的范围
        if(left < right ) {a[left] = a[right];left++;}

        while(a[left] <= x && left < right) left++;
        if(left < right ) {a[right] = a[left];right--;}
    }
    a[left] = x;
    return left;
}

void quicksort(int a[],int left,int right)
{
    if(left <  right)
    {
      int mid = adjustarray(a,left,right);
      quicksort(a,left,mid - 1);
      quicksort(a,mid + 1,right);
    }
}


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

 

posted on 2014-05-27 10:24  berkeleysong  阅读(160)  评论(0编辑  收藏  举报

导航