标签:style blog color os art for
1 #include<iostream> 2 #include<cstdlib> 3 #include<ctime> 4 using namespace std; 5 6 void swap(int *a, int *b){ 7 int tmp; 8 tmp = *a; 9 *a = *b; 10 *b = tmp; 11 } 12 13 int partition(int A[], int p, int r) 14 { 15 int x = A[r]; 16 int i = p-1; 17 int j; 18 for(j=p; j<=r-1; ++j) 19 if(A[j]<=x){ 20 i++; 21 swap(A[i], A[j]); 22 } 23 swap(A[i+1], A[r]); 24 return i+1; 25 } 26 27 int randomized_partition(int A[], int p, int r) 28 { 29 srand(unsigned(time(NULL))); 30 int i = rand()%(r-p) + p; 31 swap(A[r], A[i]); 32 return partition(A, p, r); 33 } 34 35 void quicksort(int A[], int p, int r) 36 { 37 int q; 38 if(p<r){ 39 q = partition(A, p, r); 40 quicksort(A, p, q-1); 41 quicksort(A, q+1, r); 42 } 43 } 44 45 void randomized_quicksort(int A[], int p, int r) 46 { 47 int q; 48 if(p<r){ 49 q = randomized_partition(A, p, r); 50 randomized_quicksort(A, p, q-1); 51 randomized_quicksort(A, q+1, r); 52 } 53 } 54 55 void print(int A[], int n) 56 { 57 int i; 58 for(i=0; i<n; ++i) 59 cout<<A[i]<<" "; 60 } 61 62 int main() 63 { 64 int a[]={8, 23, 34, 1231, 9898, 1231, 22, 99}; 65 randomized_quicksort(a, 0, 7); 66 print(a, 8); 67 return 0; 68 }
标签:style blog color os art for
原文地址:http://www.cnblogs.com/gnodidux/p/3840799.html