Quicksort
CS2 Course Summary Slide 29

Quicksort an artful example of the "divide and conquer" problem solving strategy. Quicksort is discussed at some length, then an assigment is issued to write a Java program based on the pseudocode.


Pseudocode

    Note1:  Assume a[n+1] is "large"!
    Note2:  Except for the word pseudocode, this is Java!

    pseudocode quicksort(int[] a, int m, int n)
    {
        if ( m < n ) {
            int i = m; int j = n + 1; int k = a[m];
            for (;;) {
                do { i++; } while ( a[i] <= k );
                do { j--; } while ( a[j] >= k );
                if ( i < j ) {
                    interchange(a,i,j);
                } else {
                    break;
                }
            }
            interchange(a,m,j);
            display("WITHIN QUICKSORT ...",a);
            quicksort(a,m,j-1);
            quicksort(a,j+1,n);
        }
    }


Write an application program which generates 30 two digit integers, stores them in an array, and sorts the array by means of the quicksort algorithm. Arrange to display the elements of the array on one line (separating them by a space) at the beginning of each execution of the quicksort method.

Proceed in the following way, working within the Sorting project:

  1. Establish the Quicksort program in the quicksort package.
  2. Compile it and run it!