Created
October 21, 2011 16:56
-
-
Save raheelahmad/1304311 to your computer and use it in GitHub Desktop.
Partitioning an array: bare bones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// partition2.java | |
// demonstrates partitioning an array | |
// uses highest-index (right) element as pivot | |
// 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 long getValue(int index) // return value at index | |
{ return theArray[index]; } | |
//-------------------------------------------------------------- | |
public void display() // displays array contents | |
{ | |
for(int j=0; j<nElems; j++) // for each element, | |
System.out.print(theArray[j] + " "); // display it | |
System.out.println(""); | |
} | |
} // end class ArrayPar | |
//////////////////////////////////////////////////////////////// | |
class PartitionApp | |
{ | |
public static void main(String[] args) | |
{ | |
int maxSize = 10; // array size | |
ArrayPar arr; // reference to array | |
arr = new ArrayPar(maxSize); // create the array | |
for(int j=0; j<maxSize; j++) // fill array with | |
{ // random numbers | |
long n = (int)(java.lang.Math.random()*99); | |
arr.insert(n); | |
} | |
arr.display(); // display original array | |
// partition the array | |
} // end main() | |
} // end class PartitionApp | |
//////////////////////////////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment