您的位置:首页 > 其它

快速排序版本二

2012-12-06 14:59 155 查看
  1 #include
<iostream>
  2 #include
<stdio.h>
  3 #include
<stdlib.h>
  4 using namespace std;
  5 
  6 int Partition(int *a, int m, int
n);
  7 
  8 void QuickSort(int *a, int low, int
high)
  9 {    
  
 10    
    int pivot;
 11    
    if(low <
high)
 12    
    {  
    
 13    
     
      pivot =
Partition(a, low, high);
 14    
     
     
QuickSort(a, low, pivot - 1);
 15    
     
     
QuickSort(a, pivot + 1, high);
 16    
    }
 17 }
 18 
 19 int Partition(int *a, int m, int n)
 20 {    
  
 21    
    int t;
 22    
    int temp = a[m];
 23    
    int i = m;
 24    
    int j = n + 1;
 25    
    while(i <
j)
 26    
    {  
    
 27    
     
      while(i
< j && a[--j]
>= temp);
 28    
     
      while(i
< j && a[++i]
<= temp);
 29    
     
      if(i
< j)
 30    
     
      {
   
  
 31    
     
     
     
  t = a[i];
 32    
     
     
     
  a[i] = a[j];
 33    
     
     
     
  a[j] = t;
 34    
     
      }
 35    
    } 
 36    
    t = a[m];
 37    
    a[m] = a[j];
 38    
    a[j] = t;
 39    
    return j;
 40 }
 41 
 42 int main()
 43 {    
  
 44    
    int
i; 
 45    
    int *a = (int
*)malloc(sizeof(int) * 10);
 46    
   
cout<<"intput 10
nuber:"<<endl;
 47    
    for(i = 0; i <
10; i++)
 48    
    {  
    
 49    
     
     
cin>>a[i];
 50    
    }
 51    
    QuickSort(a, 0, 9);
 52    
   
cout<<"After quick
sort:"<<endl;
"test07.cpp" 59L, 862C 已写入  
     
     
     
     
     
     
     
     
     
     
     15,13-27
    顶端
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: