zoukankan      html  css  js  c++  java
  • 【快速排序】算法实现

    快速排序,是分治递归的应用。算法的时间复杂性为:O(nlogn)。算法的递归深度接近于logn。因此,所需要的工作单元为O(logn)。

    //快速排序
    #include<iostream.h>
    int count=0;
    void swap(int &a,int &b)
    {
        
    int temp;
        temp
    =a;
        a
    =b;
        b
    =temp;
    }
    int split(int array[],int low,int high)
    {
        
    int i=low;//保存枢点元素的位置,初始值为low
        int j;
        
    int x=array[low];
     
        
    for(j=low+1;j<=high;j++)
        {
            
    if(array[j]<=x)
            {
                i
    ++;
                
    if(i!=j)
                {
                    swap(array[i],array[j]);
                }
            }
        }
        swap(array[low],array[i]);
        
    return i;
    }
    void print(int array[],int n)
    {  
        
    for(int i=0;i<8;i++)
        {
            cout
    <<array[i]<<" ";
        }
        cout
    <<endl;
    }
    void quick_sort(int array[],int low,int high)
    {
        
    int k;
        
    if(low<high)
        {
            k
    =split(array,low,high);     
            quick_sort(array,low,k
    -1); 
            quick_sort(array,k
    +1,high); 
        }
    }
    void main()
    {
        
    int array[]={5,8,4,9,3,6,7,2};
        quick_sort(array,
    0,7);     
        print(array,
    7);
    }
  • 相关阅读:
    [POJ 1200]Crazy Search
    [来源不详]删数方案数
    noip搜索模拟题 骰子
    [SDOI2010]地精部落
    [HAOI2008]硬币购物
    BZOJ1009: [HNOI2008]GT考试
    BZOJ1830: [AHOI2008]Y型项链 & BZOJ1789: [Ahoi2008]Necklace Y型项链
    BZOJ1251: 序列终结者
    BZOJ3620: 似乎在梦中见过的样子
    BZOJ4477: [Jsoi2015]字符串树
  • 原文地址:https://www.cnblogs.com/wintergrass/p/2031023.html
Copyright © 2011-2022 走看看