Question
Please Modify the partition.java program so that the partitionIt() method always uses the highest-index (right) element as the pivot, rather than an arbitrary number. Make
Please Modify the partition.java program so that the partitionIt() method always uses the highest-index (right) element as the pivot, rather than an arbitrary number. Make sure your routine will work for arrays of three or fewer elements.
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 while(rightPtr > 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) swap (leftPtr, right); 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 System.out.println(", Partition is at index " + partDex); System.out.println(); arr.display(); // display partitioned array } // end main() } // end class PartitionApp
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started