1

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
vsnyc
  • 2,117
  • 22
  • 35
  • I think the problem is at the `partition` function. After creating the arrays `lesser` and `greater`, you are trying to access a position greater than them, because of the loop `for(int j=0; j < a.length; j++)`, and because `a.length` = `greater.length` + `lesser.length` – Soon Ho Nov 16 '15 at 02:10
  • Thank you for replying! I will check and revert. :) – Yanamandram Kumar Nov 16 '15 at 03:20
  • You are right. It is because a.length is the sum of lesser.length and greater.length. I will correct the problem. :) – Yanamandram Kumar Nov 16 '15 at 03:27
  • Hold on..kindly allow me to ask one more thing. I am now aware of what the problem is, but it still doesn't change the fact that I need to linearly search through the array a in its entirety to find elements that are either lesser than the pivot OR greater than the pivot in terms of value. And to do that, using a.length is essential. I guess, I'll have to add some sort of sentinel value that stops the copying beyond the original lengths of lesser and greater? What would your approach to this be? – Yanamandram Kumar Nov 16 '15 at 03:38
  • Hmm..is the call in main to partition() also incorrect? I am getting an error message saying so. Well, it basically says the same thing - ArrayIndexOutOfBoundsException. But is that true of the method call to partition() from main() ? – Yanamandram Kumar Nov 16 '15 at 04:02
  • Well, you just need to create two variables to control the access to both arrays. Something like: `int x = 0, y = 0; for(int j=0; j < a.length; j++) { if(a[j] < pivot) { lesser[x] = a[j]; x++; } if(a[j] >= pivot) { greater[y] = a[j]; y++; } }` – Soon Ho Nov 16 '15 at 20:25

0 Answers0