6 Replies - 2073 Views - Last Post: 17 March 2012 - 04:12 AM Rate Topic: -----

#1 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 341
  • View blog
  • Posts: 1,281
  • Joined: 06-December 09

Quick Sort - ArrayIndexOutOfBounds Exception

Posted 16 March 2012 - 04:48 AM

Hi,

I had a conversation with a recruiter from Google the other day and he told me that before I apply (in October) for an internship I should make sure I am comfortable with algorithmsa and data structures. Since we don't learn those until next year I thought I should start now.

No matter what I do I keep getting out of bounds exceptions and cannot seem to pinpoint the problem.

My code is:


import java.util.Arrays;
import java.util.List;
import java.util.Random;


public class QuickSort {
    
    private Random rand;
    private int[] numbers;
    
    public QuickSort(int[] list)
    {
        rand = new Random();
        numbers = list;
    }

    public int[] getNumbers()
    {
        return numbers;
    }
    
    public void sort()
    {
        partition(0, numbers.length - 1);
    }
    
    private void partition(int left, int right)
    {
        System.out.print(Arrays.toString(numbers));
        int pivot = numbers[rand.nextInt() % numbers.length];
        int i = left;
        int j = right;
        while (i <= j)
        {
            while (numbers[i] < pivot)
            {
                i++;
            }
            while (numbers[j] > pivot)
            {
                j--;
            }

            if (i <= j)
            {
                swap(i,j);
                i++;
                j--;
            }
        }
        partition(j, left);
        partition(right, i);
    }

    private void swap(int i, int j) {
        int left = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = left
    }
}



I started off by using a book, then a tutorial on here... so the code is probably fairly familiar.

Any ideas what I am doing wrong?

Thanks,

Shane

Is This A Good Question/Topic? 0
  • +

Replies To: Quick Sort - ArrayIndexOutOfBounds Exception

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Quick Sort - ArrayIndexOutOfBounds Exception

Posted 16 March 2012 - 06:33 AM

I'll tell you. Your error was NOT easy to find. I had to use my debugger and everything. :P

Your problem is with THIS statement:
        int pivotIndex = rand.nextInt() % numbers.length;
        int pivot = numbers[pivotIndex];



What you may not not know is that by default, randInt() returns a value between Integer.MIN_VALUE and Integer.MAX_VALUE, which includes negative numbers. Therefore, the modulus operator is returning a negative value. To fix you problem, you simply need to absolute value your range:

        int pivotIndex = Math.abs(rand.nextInt() % numbers.length);
        int pivot = numbers[pivotIndex];



Happy coding!
Was This Post Helpful? 1
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Quick Sort - ArrayIndexOutOfBounds Exception

Posted 16 March 2012 - 06:36 AM

int pivot = numbers[rand.nextInt() % numbers.length];



The nextInt you're using can generate negative numbers. Use nextInt(numbers.length).
Was This Post Helpful? 1
  • +
  • -

#4 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 341
  • View blog
  • Posts: 1,281
  • Joined: 06-December 09

Re: Quick Sort - ArrayIndexOutOfBounds Exception

Posted 16 March 2012 - 06:42 AM

Neither solutions seem to have fixed it completely, but hopefully it brings me closer to it working! Thanks, I didn't even think about negative numbers haha.
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1009
  • View blog
  • Posts: 2,186
  • Joined: 05-May 05

Re: Quick Sort - ArrayIndexOutOfBounds Exception

Posted 16 March 2012 - 06:47 AM

Here's a working sort if you want a reference.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Quick Sort - ArrayIndexOutOfBounds Exception

Posted 16 March 2012 - 06:49 AM

I don't know what's going on totally. You should use your debugger and monitor the left and right variables. they're doing some pretty funky stuff. I suspect you need to reevaluate your loops. Instead of using loops, think this one through totally with recursion.
Was This Post Helpful? 0
  • +
  • -

#7 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 341
  • View blog
  • Posts: 1,281
  • Joined: 06-December 09

Re: Quick Sort - ArrayIndexOutOfBounds Exception

Posted 17 March 2012 - 04:12 AM

Well I managed to get one of the second years to go through it with me on IRC at about 2am haha... here is the fixed code for anybody in a similar situation.

import java.util.Random;


public class QuickSort implements Algorithm {

    private Random rand;
    private int[] numbers;

    public QuickSort(int[] list)
    {
        rand = new Random();
        numbers = list;
    }

    public int[] getNumbers()
    {
        return numbers;
    }

    public void sort()
    {
        partition(0, numbers.length-1);
    }

    private void partition(int left, int right)
    {
        int pivotIndex = Math.abs(rand.nextInt() % numbers.length);
        int pivot = numbers[pivotIndex];
        int i = left;
        int j = right;
        while (i <= j)
        {
            while (numbers[i] < pivot)
            {
                i++;
            }
            while (numbers[j] > pivot)
            {
                j--;
            }

            if (i <= j)
            {
                swap(numbers,i,j);
                i++;
                j--;
            }
        }

        if(j > left)
            partition(left,j);

        if(i < right)
            partition(i, right);

    }

    private void swap(int[] list, int i, int j) {
        int left = list[i];
        list[i] = list[j];
        list[j] = left;
    }
}



I am going to create a viewer/tester and go through all the algorithms that I can find, then github it.

EDIT:

Using the random pivot meant that the time it takes varies a lot. By changing int pivotIndex = Math.abs(rand.nextInt() % numbers.length); to int pivotIndex = (left + right) / 2; on average I seem to be saving about 50000 nanoseconds.

This post has been edited by Shane Hudson: 17 March 2012 - 05:53 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1