// partition.java // demonstrates partitioning an array // to run this program: C>java PartitionApp //////////////////////////////////////////////////////////////// class ArrayPar { private long[] theArray; // ref to array theArray private int nElems; // number of data items //-------------------------------------------------------------- public ArrayPar(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 int size() // return number of items { return nElems; } //-------------------------------------------------------------- public void display() // displays array contents { System.out.print("A="); for(int j=0; j left && // find smaller item 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) return leftPtr; // return partition } // end partitionIt() //-------------------------------------------------------------- public void swap(int dex1, int dex2) // swap two elements { long temp; temp = theArray[dex1]; // A into temp theArray[dex1] = theArray[dex2]; // B into A theArray[dex2] = temp; // temp into B } // end swap() //-------------------------------------------------------------- } // end class ArrayPar //////////////////////////////////////////////////////////////// class PartitionApp { public static void main(String[] args) { int maxSize = 16; // array size ArrayPar arr; // reference to array arr = new ArrayPar(maxSize); // create the array for(int j=0; j