zoukankan      html  css  js  c++  java
  • 快速排序

    1. #include <cstdio>  
    2. #define ARRLEN(x) (sizeof(x)/sizeof(int))  
    3. int partition(int a[], int low, int high)  
    4. {  
    5.     int pivotkey=a[low];  
    6.     while (low<high)  
    7.     {  
    8.         while (low<high && a[high]>=pivotkey)  
    9.             --high;  
    10.         if (low<high)   
    11.             a[low++]=a[high];  
    12.         while (low<high && a[low]<=pivotkey)  
    13.             ++low;  
    14.         if (low<high)   
    15.             a[high--]=a[low];  
    16.     }  
    17.     a[low]=pivotkey;  
    18.     return low;  
    19. }  
    20. void quicksort(int a[], int low, int high)  
    21. {  
    22.     int pivotpos;  
    23.     if (low<high)  
    24.     {  
    25.         pivotpos=partition(a,low,high);  
    26.         quicksort(a,low,pivotpos-1);  
    27.         quicksort(a,pivotpos+1,high);  
    28.     }  
    29. }  
    30. int main()  
    31. {  
    32.     int a[]={2,7,9,3,1,4,8,0,6,2};  
    33.     int i;  
    34.     for (i=0; i<ARRLEN(a); printf("%2d",a[i]),++i);  
    35.     printf("/n");  
    36.     quicksort(a,0,ARRLEN(a)-1);  
    37.     for (i=0; i<ARRLEN(a); printf("%2d",a[i]),++i);  
    38.     printf("/n");  
    39.     return 0;  
    40. }  
  • 相关阅读:
    十万个为什么
    安装VmwareTools遇到的问题
    CentOS7没有ifconfig命令怎么办
    ftp/ http/ https/ tcp的关系
    C/S和B/S架构
    Nginx 安装以及验证教程
    osi七层模型
    在linux上安装tenginx
    Awvs、Snort的下载安装
    Laravel——DI(依赖注入)
  • 原文地址:https://www.cnblogs.com/small-strong/p/3922276.html
Copyright © 2011-2022 走看看