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. }  
  • 相关阅读:
    Vagrant安装virtualbox
    SQLSERVER排查CPU占用高的情况
    删除重复记录,只留一条
    ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(34)、chr(39)、……
    手机和PC端的录屏软件
    2017年初面试总结
    Python面向对象
    Python字体颜色
    Python第二模块总结
    Fiddler使用教程(转)
  • 原文地址:https://www.cnblogs.com/small-strong/p/3922276.html
Copyright © 2011-2022 走看看