zoukankan      html  css  js  c++  java
  • 算法导论 快速排序

    #include<iostream>
    #include<algorithm>
    using namespace std;
    int partition(int* &a,int p,int r)
    {
    int x=a[r];
    int i=p-1;
    for(int j=p;p<=r-1;p++)
    {
    if(a[j]<=x)
    {
    i=i+1;
    swap(a[i],a[j]);
    }
    }
    swap(a[i+1],a[r]);
    return i+1;
    }
    void quicksort(int* &a,int p,int r)
    {
    if(p<r)
    {
    int q= partition(a,p,r);
    quicksort(a,p,q-1);
    quicksort(a,q+1,r);
    }
    }

    int main()
    {
    const int LEN = 20;
    int b[LEN] = {12, 43, 0, -4, 98, 75, 64, 88, 5, 32, 11, 12, 13, 84, 34, 27, -5, -244, 49, 345};
    int* a = new int[LEN];
    for(int i = 0; i < LEN; i++)
    a[i] = b[i];
    quicksort(a, 0, LEN - 1);
    for(int i = 0; i < LEN; i++)
    cout<<a[i]<<endl;
    return 0;
    }
  • 相关阅读:
    bzoj 3924
    bzoj 1095
    luogu 4886
    bzoj 2152
    CF960G
    bzoj 3561
    bzoj 4176
    bzoj 4407
    bzoj 3309
    luogu 4608
  • 原文地址:https://www.cnblogs.com/riverphoenix/p/2406623.html
Copyright © 2011-2022 走看看