zoukankan      html  css  js  c++  java
  • Quick Sort

      

     1 #include <iostream>
     2 using namespace std;
     3 int partition(int *Ary,int low ,int high){
     4     int temp;
     5     int pivotkey=Ary[low];
     6     while(low<high){
     7         while(low<high && Ary[high]>=pivotkey){
     8             high--;
     9         }
    10         temp=Ary[low];
    11         Ary[low]=Ary[high];
    12         Ary[high]=temp;
    13         while(low<high && Ary[low]<=pivotkey){
    14             low++;
    15         }
    16         temp=Ary[low];
    17         Ary[low]=Ary[high];
    18         Ary[high]=temp;
    19     }
    20     return low;
    21 }
    22 void Qsort(int *Ary,int low,int high){
    23     if(low<high){
    24         int key=partition(Ary,low,high);
    25         
    26         Qsort(Ary,low,key-1);
    27         Qsort(Ary,key+1,high);
    28     }
    29     
    30 }
    31 int main(int argc, char *argv[]) {
    32     
    33     cout<<"Plz enter 10 numbers which then will be sorted"<<endl;
    34     int Ary[10] ;
    35     for (int i=0;i<10;i++){
    36         cin>>Ary[i];
    37     }
    38     int length=(sizeof(Ary)/sizeof(int));
    39     Qsort(Ary,0,length-1);
    40     for(int j=0;j<10;j++){
    41         cout<<Ary[j]<<" ";
    42     }
    43     return 0;
    44     
    45 }
  • 相关阅读:
    限制泛型可用类型
    泛型的常规用法(声明两个类型)
    一个类似于金字塔的图形
    Fibonacci数
    快来秒杀我
    奇偶数分离
    Background
    Financial Management
    HangOver
    Binary String Matching
  • 原文地址:https://www.cnblogs.com/wuruofeng/p/9786500.html
Copyright © 2011-2022 走看看