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

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 void MyQuickSort(int *pArray,int Start,int End)
     5 {
     6                 int key = pArray[Start];
     7                 int low = Start,high = End;
     8                 while(low < high)
     9                 {
    10                         while(low < high && key < pArray[high])
    11                         {
    12                                     high--;
    13                         }
    14                         pArray[low] = pArray[high];
    15                         while(low < high && key > pArray[low])
    16                         {
    17                                   low++;
    18                         }
    19                         pArray[high] = pArray[low];
    20                 }
    21                 pArray[low] = key;
    22                 if(Start < low)
    23                 MyQuickSort(pArray,Start,low);
    24                 if(End > low+1)
    25                 MyQuickSort(pArray,low+1,End);
    26 }
    27 
    28 int main()
    29 {
    30     int pArray[20],nCount;
    31     while(~scanf("%d",&nCount) && nCount)
    32     {
    33                                for(int i = 0; i<nCount && i<20;i++)
    34                                {
    35                                        scanf("%d",&pArray[i]);
    36                                }
    37                                MyQuickSort(pArray,0,nCount-1);
    38                                for(int i = 0;i<nCount && i<20;i++)
    39                                {
    40                                        printf("%d ",pArray[i]);
    41                                }
    42     }
    43     
    44     return 0;
    45 }
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 
     4 void MyQuickSort(int *pArray,int Start,int End)
     5 {
     6                 int key = pArray[Start];
     7                 int low = Start,high = End;
     8                 while(low < high)
     9                 {
    10                         while(low < high && key < pArray[high])
    11                         {
    12                                     high--;
    13                         }
    14                         pArray[low] = pArray[high];
    15                         while(low < high && key > pArray[low])
    16                         {
    17                                   low++;
    18                         }
    19                         pArray[high] = pArray[low];
    20                 }
    21                 pArray[low] = key;
    22                 if(Start < low)
    23                 MyQuickSort(pArray,Start,low);
    24                 if(End > low+1)
    25                 MyQuickSort(pArray,low+1,End);
    26 }
    27 
    28 int main()
    29 {
    30     int pArray[20],nCount;
    31     while(~scanf("%d",&nCount) && nCount)
    32     {
    33                                for(int i = 0; i<nCount && i<20;i++)
    34                                {
    35                                        scanf("%d",&pArray[i]);
    36                                }
    37                                MyQuickSort(pArray,0,nCount-1);
    38                                for(int i = 0;i<nCount && i<20;i++)
    39                                {
    40                                        printf("%d ",pArray[i]);
    41                                }
    42     }
    43     
    44     return 0;
    45 }
  • 相关阅读:
    9.17 HTML CSS
    9.16
    9.15
    9.14
    9.13
    9.12
    9.11
    9.10
    9.9
    9.8
  • 原文地址:https://www.cnblogs.com/FWFC/p/6282496.html
Copyright © 2011-2022 走看看