• 快速排序


      1 #include <stdio.h>
      2 
      3 #define CUTOFF 3
      4 
      5 typedef int ElementType;
      6 
      7 void SwapTwoNum(ElementType *Num_1,ElementType *Num_2)
      8 {
      9     int NumTemp = *Num_1;
     10     *Num_1 = *Num_2;
     11     *Num_2 = NumTemp;
     12 }
     13 
     14 // Return Median of left,center,and right
     15 //order these and hide the pivot
     16 ElementType Median3(ElementType *Array,int Left,int Right)
     17 {
     18     int Center = (Left + Right) / 2;
     19     
     20     // C >= L,R >= L,R >= C ---> L <= C <= R
     21     if(Array[Left] > Array[Center])
     22     {
     23         SwapTwoNum(&Array[Left],&Array[Center]);
     24     }
     25     if(Array[Left] > Array[Right])
     26     {
     27         SwapTwoNum(&Array[Left],&Array[Right]);
     28     }
     29     if(Array[Center] > Array[Right])
     30     {
     31         SwapTwoNum(&Array[Center],&Array[Right]);
     32     }
     33     
     34     //Hide pivot
     35     SwapTwoNum(&Array[Center],&Array[Right-1]);
     36     return Array[Right-1];
     37 }
     38 
     39 void InsertionSort(ElementType *Array,int ArrayLen)
     40 {
     41     int i,j;
     42     ElementType ExtractElem;
     43     for(i = 1;i < ArrayLen;i ++)
     44     {
     45         ExtractElem = Array[i];
     46         for(j = i;j > 0 && ExtractElem < Array[j-1];j --)
     47         {
     48             Array[j] = Array[j-1];
     49         }
     50         //Insert
     51         Array[j] = ExtractElem;
     52     }
     53 }
     54 
     55 void QSort(ElementType *Array,int Left,int Right)
     56 {
     57     int i,j;
     58     ElementType Pivot;
     59     
     60     if(Left + CUTOFF <= Right)
     61     {
     62         Pivot = Median3(Array,Left,Right);
     63         i = Left;j = Right - 1;
     64         while(1)
     65         {
     66             while(Array[++i] < Pivot)
     67                 ;
     68             while(Array[--j] > Pivot)
     69                 ;
     70             if(i < j)
     71             {
     72                 SwapTwoNum(&Array[i],&Array[j]);
     73             }
     74             else
     75             {
     76                 break;
     77             }
     78         }
     79         
     80         //restore pivot
     81         SwapTwoNum(&Array[i],&Array[Right-1]);
     82         
     83         QSort(Array,Left,i-1);
     84         QSort(Array,i+1,Right);
     85     }
     86     else
     87     {
     88         InsertionSort(Array+Left,Right-Left+1);
     89     }
     90 }
     91 
     92 void QuickSort(ElementType *Array,int ArrayLen)
     93 {
     94     QSort(Array,0,ArrayLen-1);
     95 }
     96 
     97 int main()
     98 {
     99     ElementType TestArray[10] = {718,224,3332,4443,55,31,66,79,90,7};
    100     QuickSort(TestArray,10);
    101     int i;
    102     for(i = 0;i < 10;i ++)
    103     {
    104         printf("%d ",TestArray[i]);
    105     }
    106     printf("
    ");
    107     return 0;
    108 }
  • 相关阅读:
    K8s中Secrets
    记一次kubernetes配置secret拉取私仓镜像错误
    K8S中ConfigMap
    阿里云RDSforMySQL如何定位本地IP
    Python3运算符
    nyoj 67-三角形面积 (海伦公式, 叉积)
    nyoj 66-分数拆分 (Java,暴力)
    nyoj 65-另一种阶乘问题 (Java 高精度)
    nyoj 64-鸡兔同笼 (解二元一次方程)
    nyoj 63-小猴子下落 (模拟)
  • 原文地址:https://www.cnblogs.com/Asurudo/p/9427437.html
Copyright © 2020-2023  润新知