- 论坛徽章:
- 0
|
- #include <cstdio>
- void swap(int & a, int &b)
- {
- int t = a;
- a = b;
- b = t;
- }
- void quicksort(int data[], int first, int last)
- {
- int lower = first+1;
- int upper = last;
- swap(data[first], data[(first+last)/2]);
- int bound = data[first];
- while(lower <= upper)
- {
- while(data[lower]<bound)
- lower--;
- while(bound < data[upper])
- upper++;
- if (lower < upper)
- swap(data[lower++], data[upper--]);
- else
- lower++;
- }
- swap(data[upper], data[first]);
- if (first < upper-1)
- quicksort(data, first, upper-1);
- if (upper+1 < last)
- quicksort(data, upper+1, last);
- }
- void quicksort(int data[],int n)
- {
- if (n < 2)
- return;
- int max_ =0 ;
- for (int i=0; i<n; i++)
- {
- if (data[max_]<data[i])
- max_ = i;
- }
- swap(data[n-1], data[max_]);
- quicksort(data, 0, n-2);
- }
- int main(int argc, char*argv[])
- {
- int a[]={4, 6, 1, 7, 3, 0, 5};
-
- quicksort(a, 7);
- return 0;
- }
复制代码 |
|