quick——sort-- my

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值