算法导论(三) 高速排序

快排


直接上代码。数学推导以后再上

另一版随机化版本号的高速排序

#include <iostream>

using namespace std;


void _swap(int a[] , int i , int j)//交换函数
{

    int temp;
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}


int  Partition(int *a,int p,int r)//划分程序
{
    int x,i;
    x=a[r];
    i=p-1;
    for(int j=p;j<r;j++)
    {
        if(a[j]<=x){
            i+=1;
            _swap(a,i,j);
        }

    }
    _swap(a,i+1,r);
        return i+1;
}

void QuickSort(int *a,int p,int r)//
{
    int q;
    if(p<r){
        q=Partition(a,p,r);
        QuickSort(a,p,q-1);
        QuickSort(a,q+1,r);
    }
}



int main()
{
    int a[9];
    for(int k=0;k<9;k++)
        cin>>a[k];

    QuickSort(a,0,8);//入口
    for(int k=0;k<9;k++)
        cout<<a[k]<<"-----"<<endl;
    return 0;
}


原文地址:https://www.cnblogs.com/lxjshuju/p/6914977.html