I thought I could just share some code I just decided to do for a bit of fun. It deals with the problem of array partitioning and I have used Java to implement the solution. I am originally a C/C++ guy, and I would like some guidance as to the reason the java.lang.ArrayIndexOutOfBoundsException is appearing when I try to run this.
Here's my code:
/**
* This program implements an algorithm for partitioning an array of doubles.
*
* @Yanamandram Kumar
* @1.0
*/
import java.util.*;
import java.lang.*;
public class ArrayPartitioning
{
public static double[] partition(double[] a) {
Scanner input1 = new Scanner(System.in);
double pivot = 0.0; //We'll assign a proper value to this later.
double[] lesser;
double[] greater;
int lesser_count=0, greater_count=0;
int loopVar = 0;
double[] result = new double[a.length];
int user_choice = 0; //This will be used to take the user's choice for the pivot element in the array.
System.out.println("Inside partitioning method. There are "+a.length+" elements in the array. Please enter your choice for the pivot element: ");
user_choice = input1.nextInt();
while((user_choice <= 0) && (user_choice > a.length)) {
System.out.println(" Invalid choice value. Please re-enter your choice value: ");
user_choice = input1.nextInt();
}//Any choice that is 0 or negative, OR is greater than the length of the array will not be permitted.
pivot = a[user_choice - 1]; //Now the pivot is set.
//While partitioning, ALL elements that are lesser than OR greater than the pivot must first be counted.
for(int i=0; i < a.length; i++){
if(a[i] < pivot) {
lesser_count++;
}
if(a[i] >= pivot) {
greater_count++;
}
}
//Now we allocate memory for the arrays lesser and greater based on these values.
lesser = new double[lesser_count];
greater = new double[greater_count];
//Lets begin partitioning shall we?
for(int j=0; j < a.length; j++) {
if(a[j] < pivot) {
lesser[j] = a[j];
}
if(a[j] >= pivot) {
greater[j] = a[j];
}
}
for(loopVar=0; loopVar < lesser.length; loopVar++) {
result[loopVar] = lesser[loopVar];
}
//Now we copy the pivot element into the result array.
result[loopVar] = pivot;
loopVar = lesser.length + 1;
//Now we copy the elements that are greater than the pivot into the result array.
for(int k =0; k < greater.length; k++) {
result[loopVar] = greater[k];
loopVar++;
}
return result;
}//End of method partition()
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int array_size = 0;
double[] num_arr;
double[] result_arr;
System.out.println(" Enter the size of the array you need to create and partition : ");
array_size = input.nextInt();
num_arr = new double[array_size];
for(int i=0; i<array_size; i++) {
System.out.println(" Enter the value to be entered into the array: ");
num_arr[i] = input.nextDouble(); //Even if an integer is entered, it will forcibly be cast into a double before being assigned.
while(num_arr[i] < 0.0) {
System.out. println(" Invalid value entered. Please enter a positive value: ");
num_arr[i] = input.nextDouble(); //Even if an integer is entered, it will forcibly be cast into a double before being assigned.
}//Negative doubles will not be allowed.
} //This creates the array for our purposes.
result_arr = new double[num_arr.length];
result_arr = partition(num_arr);
System.out.println(" Now printing the partitioned array: ");
System.out.println(" ");
for(int i=0; i < result_arr.length; i++) {
System.out.print(""+result_arr[i]+" ");
}
}//End of main()
}//End of the class