0

For my programming class we need to write a method to find the median number in an array. I understand the basic concept that to find the median you just need to find the kth smallest number where 'k equals array.size/2', but I cannot get my code to work.

My task is to find the median of the first partition of the array. For this assignment we have to use the rightmost value as the pivot and the partition method has to return the index where the values are higher than the pivot.

Then we have to write a recursive method kthSmallest method that has three cases:

  • The size of the subarray of values smaller than the pivot is equal to k - this is the base case.
  • The subarray of smaller values is larger than k-1 so the kth smallest is in the subarray of smaller values.
  • The subarray of smaller values is smaller than k-1 so the kth smallest is in the subarray of larger values.

This is what I have so far, but I get

java.lang.ArrayIndexOutOfBoundsException: -1

in my partition method because for some reason end is eventually -1, so it tries to find arr[-1].

class Median{
    static int kthSmallest(int[] arr, int left, int right, int k){
        int divider = partition(arr, left, right);
        if(divider == k-1){
            System.out.println("Case1/Base");
            return arr[divider];
        }
        if(divider > k-1){
            System.out.println("Case2");
            return kthSmallest(arr, left, divider, k);
        }
        else{
            System.out.println("Case3");
            return kthSmallest(arr, divider + 1, right, k - (divider +1));
        }
    }

    static int partition(int[] arr, int left, int right){
        int begin = left;
        System.out.println("Begin: " + begin);
        int end = right;
        System.out.println("End: " + end);
        int pivot = arr[right];
        System.out.println("Pivot: " + pivot);
        while(begin < end){
            while(arr[begin] < pivot){
                begin++;
            }
            while(arr[end] >= pivot){
                end--;
            }
            swap(arr, begin, end);
        }
        swap(arr,begin,end);
        System.out.println("return: " + begin);
        printArr(arr);
        return begin - 1;
    }

    static void swap(int[] arr, int i, int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    static void printArr(int[] arr){
        System.out.println();
        for(int index = 0; index < arr.length - 1; index++){
            System.out.print(arr[index] + ", ");
        }
        System.out.print(arr[arr.length-1]);
        System.out.println();
    }

    public static void main(String[] args){
        int[] arr = {34,-1,0,5,3};
        printArr(arr);
        //partition(arr, 0, 2);
        //printArr(arr);
        System.out.println(kthSmallest(arr, 0, arr.length - 1, 1));

    }

}
zx485
  • 28,498
  • 28
  • 50
  • 59
hjskeqwe
  • 175
  • 1
  • 9
  • Why do you say you get a null pointer, when you actually get a `java.lang.ArrayIndexOutOfBoundsException: -1`? – Andreas Mar 22 '16 at 16:17
  • Why do you expect the code `while(arr[end] >= pivot){ end--; }` would not potentially cause that exception? If all the values are `>= pivot`, then of course it causes that exception. Have you tried debugging the code? Your `print` statement even says `Pivot: -1`, so all values truly *are* `>= pivot`. – Andreas Mar 22 '16 at 16:20
  • Oh I used the wrong term by accident I will edit that. – hjskeqwe Mar 22 '16 at 16:21
  • Ok I see what you are saying. My partition method worked for some other cases so I was dumb and assumed it was correct and that the error was caused by something else. Thanks for the help! I'll see if I can figure out how to fix it. – hjskeqwe Mar 22 '16 at 16:31
  • Try [this](https://gist.githubusercontent.com/SumitJainUTD/4e5f7374237d33c64c36/raw/de83ad12965a093eca032a74a84b26958469353d/KthSmallestElementInArray.java), uses [priority queue](https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html). – Daniel Mar 22 '16 at 16:37
  • That definitely seems easier but for this assignment we are supposed to do it using a partition method like I have above so I don't think I could use the implementation you linked. Thanks though! – hjskeqwe Mar 22 '16 at 16:42

0 Answers0