quick——sort-- my

来源:互联网 发布:申请淘宝企业店铺条件 编辑:程序博客网 时间:2024/06/03 21:49
#include <iostream>

using namespace std;
//quick sort
/*
Qsort(int a[],int l, int r)
{
     // destribute it
     //if(l<r)//递归调用的返回条件
     {
        int pit = (a,l,r)
        Qsort(a,l,pit-1);
        Qsort(a,pit+1,r);
     }
}ggt

*/
void my_swap(int &a, int &b)
{// 1 ^ 0 = 1; 相同为0 ; 相异 取 或;0 ^ 1 = 0;
    /*a = a^b;
    b = a^b;
    a = a^b;*/
    int temp = b;
    b=a;
    a=temp;
}
void my_swa22p(int &a, int &b)
{
    a = a^b;
    b = a^b;
    a = a^b;
}
int partit(int a[],int l,int r)
{
    // r is the same of length
    //1 = get the pivot element
    //2 = divide

    int pivot = a[r];

    int j = l;
    int i = j-1;

    for(int j = l;j<=r-1;++j)
    {
        if(a[j]<pivot)
        {
            i++;// point to one which is the first big than pivot
            my_swap(a[i],a[j]);
        }
        if(a[j]>=pivot)
        {
            continue;
        }
    }
    my_swap(a[i+1],a[r]);//这里要进行修改 ··!!!!!
    return i+1;
}
void QuickSort(int a[],int l,int r)
{
    if(l<r)
    {
        int pit = partit(a,l,r);//put the pivot element in the right pit position
        QuickSort(a,l,pit-1);
        QuickSort(a,pit+1,r);//**
    }
}
int main ()
{
    int arr1 []={-1,3,4,1,3};
    QuickSort(arr1,1,4);
    int size=sizeof(arr1)/sizeof(arr1[0]);
    for(int i=1;i<size;++i)
        cout<<arr1[i]<<' ';

    int a = 3;int b = 3;
    //cout <<"a = "<<a << "b= "<<b<<endl;
    //my_swa22p(a,a);
    //cout <<"_a = "<<a << "_b= "<<a<<endl;


    system("pause");
    return 0;
}
原创粉丝点击