next next up down toc toc mail

8.8.  Quicksort

The classic quicksort algorithm is a divide-and-conquer sorting method. See Hoare, C. A. R. ``Quicksort,'' Computer J., October 1962 and Knuth, D. E. Sorting and Searching, The Art of Computer Programming, volume 3. Addison-Wesley, Reading, MA, 1973. As such, it would seem to be amenable to a concurrent implementation, and with a slight modification (actually an improvement of the standard algorithm) this turns out to be the case.

The standard algorithm begins by picking some item from the list and using this as the splitting key. A loop is entered which takes the splitting key and finds the point in the list where this item will ultimately end up once the sort is completed. This is the first splitting point. While this is being done, all items in the list which are less than the splitting key are placed on the low side of the splitting point, and all higher items are placed on the high side. This completes the first divide. The list has now been broken into two independent lists, each of which still needs to be sorted.

We sort an array numbers, with elements from 0 to n (n+1 numbers). Our algorithm is specified not just in terms of the array, numbers, but the elements over which we wish to sort lo and hi. This is so we can recurse to sort subsets of the array. We start with a sort of
quicksort( numbers, 0, n).

The algorithm as a Nassi Shneidermann diagram:

Procedure void quicksort(array numbers, int lo, int hi )

[picture]

The xSortLab Applet

This applet has really great graphics. Bubble Sort, Selection Sort, Insertion Sort, Merge Sort and Quick Sort could be visualized. Numbers from 1 to 20 are randomly arranged. You could step through (only forward) this algorithm animation or let it run till it's finished. Comparisons and Changes are counted.

The C-Source code:

 1      #include <stdio.h>
 2      
 3      /* quicksort algorithm                          */
 4      void qsort(int numbers[], int lo, int hi )
 5      {
 6      int i,j, guessed_median, help;
 7      
 8          i = lo;
 9          j = hi;
10          guessed_median = numbers[(int) ( (lo+hi) / 2 )];
11      
12          while ( i < j )    {
13              while ( numbers[i] < guessed_median )
14                  i++;
15              while ( numbers[j] > guessed_median )
16                  j--;
17              if ( i<=j )    {
18                  help = numbers[i];
19                  numbers[i] = numbers[j];
20                  numbers[j] = help;
21                  i++;
22                  j--;
23              }
24          }
25          if ( lo < j )
26              qsort(numbers, lo, j);
27          if ( i < hi )
28              qsort(numbers, i, hi);
29      }
30      
31      /* print the array                      */
32      void print(int numbers[], int limit )
33      {    int index;
34      
35          for ( index = 0; index < limit; index++ )
36              printf("%d ", numbers[index]);
37          printf("\n");
38      }
39      
40      
41      int main(void)
42      {   int numbers[10] = { 1, -3, 4,
43             99, 42, 12, 13, 13, -99, -42 };
44          printf("Qsort:\n");
45          print(numbers, 10 );
46          qsort(numbers, 0, 9 );
47          print(numbers, 10 );
48          exit(0);
49      }

An example:

%  qsort
1 -3 4 99 42 12 13 13 -99 -42 
-99 -42 -3 1 4 12 13 13 42 99 


back next up down toc toc mail


Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1998).

Last modified: 27/July/98 (12:14)