// randomizedQuickSort.java // This program is obtained by making a small modification to // Lafore's quickSort1.java. In this program, quick sort has // been implemented so that it picks a pivot element, uniformly // at random from the current array. // Programmer: Sriram Pemmaraju, 10/20/2005 //////////////////////////////////////////////////////////////// import java.util.*; // imported so that Random class can be used class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of data items //-------------------------------------------------------------- public ArrayIns(int max) // constructor { theArray = new long[max]; // create the array nElems = 0; // no items yet } //-------------------------------------------------------------- public void insert(long value) // put element into array { theArray[nElems] = value; // insert it nElems++; // increment size } //-------------------------------------------------------------- public void display() // displays array contents { System.out.print("A="); for(int j=0; j 0 && theArray[--rightPtr] > pivot) ; // (nop) if(leftPtr >= rightPtr) // if pointers cross, break; // partition done else // not crossed, so swap(leftPtr, rightPtr); // swap elements } // end while(true) swap(leftPtr, right); // restore pivot return leftPtr; // return pivot location } // end partitionIt() //-------------------------------------------------------------- public void swap(int dex1, int dex2) // swap two elements { long temp = theArray[dex1]; // A into temp theArray[dex1] = theArray[dex2]; // B into A theArray[dex2] = temp; // temp into B } // end swap( //-------------------------------------------------------------- } // end class ArrayIns //////////////////////////////////////////////////////////////// class randomizedQuickSort { public static void main(String[] args) { int maxSize = 16; // array size ArrayIns arr; arr = new ArrayIns(maxSize); // create array for(int j=0; j