首页 > 代码库 > QuickSort

QuickSort

 1 #include<iostream>
 2 
 3 
 4 using namespace std;
 5 
 6 void Repeat(int* a,int Left,int Right);
 7 int QuickSort(int* a,int Left,int Right);
 8 
 9 
10 void main()
11 {
12     int a[5] = {9,1,2,8,4}; 
13 
14     Repeat(a,0,4);
15 
16     int i = 0;
17 
18     for(i=0;i<5;i++)
19     {
20         cout<<a[i]<<" ";
21     
22     
23     }
24     cout<<endl;
25 
26 
27 
28 }
29 void Repeat(int* a,int Left,int Right)
30 {
31     int m = 0;
32     if(Left<Right)
33     {
34         m = QuickSort(a,Left,Right);
35         
36         Repeat(a,Left,m-1);
37 
38         Repeat(a,m+1,Right);
39     
40     }
41 
42 
43 }
44 int QuickSort(int* a,int Left,int Right)
45 {
46     int Temp = a[Left];
47     while(Left<Right)
48     {
49         while(Left<Right&&Temp<=a[Right])     
50         {
51             Right--;
52         
53         
54         }
55         a[Left] = a[Right];
56 
57         while(Left<Right&&Temp>=a[Left])
58         {
59         
60             Left++;
61         
62         }
63         a[Right] = a[Left];
64         a[Left] = Temp;
65     
66     }
67 
68     return Left;
69 
70 }