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 }
  • 相关阅读:
    C#文件操作
    WPF 工具提示(ToolTip)
    C#中is 和 as 操作符
    C#线程同步——lock,Monitor,Mutex(摘录)
    随记
    实现RichTextBox内容自动滚动(WPF)
    初探MVC3(一)
    thinkPad系列的笔记本质量已经大不如以前了,太令人失望了
    WPF—— ComboBox绑定应用
    new,virtual,override,interface,delegate,eventC#——方法实现总结
  • 原文地址:https://www.cnblogs.com/FWFC/p/6282496.html
Copyright © 2011-2022 走看看