zoukankan      html  css  js  c++  java
  • 快排

    emmm,这东西也很简单

    就是对于l,r把第一个数放到他应该放的位置

    再把l,r分成两个区间再进行排序

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #define maxn 10000
    
    using namespace std;
    
    int a[maxn];
    
    void Swap(int&a,int&b)
    {
        if (a==b)
            return;//这个是为了防止a和b都是a[i]的情况,这种情况下,会把a[i]变成0
        a^=b;b^=a;a^=b;
        return;
    }
    
    void qsort(int l,int r)
    {
        if (l>r)
            return;
    
        int save = a[l],low=l,high=r;
    
        while(low!=high)
        {
            while(a[high]>=save&&low<high)
                high--;
    
            while(a[low]<=save&&low<high)
                low++;
    
            if (low<high)
                Swap(a[low],a[high]);
        }
    
        Swap(a[l],a[low]);
    
        qsort(l,low-1);
        qsort(low+1,r);
        return;
    }
    
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            for (int k=1;k<=n;k++)
                scanf("%d",&a[k]);
    
            qsort(1,n);
    
            for (int k=1;k<=n;k++)
                printf("%d ",a[k]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    curl库使用文件传输
    linux 命令
    第三方库交叉编译
    指针越界
    GetWindowRect GetClientRect
    libevent
    C#关闭窗体
    C# log日志窗口
    C++同一时刻仅允许一个实例,包含多用户的场景。
    C# 引用类型
  • 原文地址:https://www.cnblogs.com/shensobaolibin/p/8109537.html
Copyright © 2011-2022 走看看