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

New Topic/Question
Reply



MultiQuote






|