快排的一种相当简单但不算最快的实现方式

 1 void quicksort(int v[], int n)
 2 {
 3     int i, last;
 4     if (n <= 1) /* nothing to do */
 5         return;
 6     swap(v, 0, rand() % n);
 7     last = 0;
 8     for (i = 1; i < n; i++)
 9         if (v[i] < v[0])
10             swap(v, ++last, i);
11     swap(v, 0, last);
12     quicksort(v, last);
13     quicksort(v+last+1, n-last-1);
14 }
15 
16 /* swap: interchage v[i] and v[j] */
17 void swap(int v[], int i, int j)
18 {
19     int temp;
20     temp = v[i];
21     v[i] = v[j];
22     v[j] = temp;
23 }                                                            

 

 

posted @ 2014-03-22 07:06  thinker-lj  Views(135)  Comments(0Edit  收藏  举报