-3

I'm copying this algorithm straight from a book, but I keep getting an ArrayIndexOutOfBoundsException at the "ERROR HERE!!!!" part... for the T[i] = ...

It's driving me nuts and I need to get this done... can someone suggest how I can fix this? I also commented other potential errors due to translating the algorithm from the book...

Heading to work, be back in a while. Help would be very very much appreciated..

public static int select(int n, int S[], int k)
    {
        return select4(S, 1, n, k);
    }

    public static int select4(int S[], int low, int high, int k)
    {
        int pivotpoint = (low+high)/2;  //the algorithm didn't define "pivotpoint", so I'm assuming I can use anything..
        if(high==low)
            return (S[low]);
        else {
            partition4(S, low, high, pivotpoint);
            if(k==pivotpoint)
                return(S[pivotpoint]);
            else if(k<pivotpoint)
                return(select4(S, low, pivotpoint-1, k));
            else
                return(select4(S, pivotpoint+1, high, k));
        }
    }

    public static void partition4(int S[], int low, int high, int pivotpoint)
    {
        int arraysize = high - low + 1;
        int r = (int)Math.ceil(arraysize/5);  //maybe error because not double?
        int i, j, mark=0, first, last;
        int pivotitem;
        int[] T = new int[r];
        int temp;

        for(i=1; i<=r; i++) {
            first = low + 5*i - 5;
            last = Math.min(low + 5*i - 1, arraysize);
            T[i] = S[(first+last)/2]; //Algorithm says "median of S[first] through S[last]"
//          ^ ERROR HERE!!!!!!!!!!!!
        }

        pivotitem = select(r, T, (int)Math.floor((r+1)/2)); //maybe error because not double?
        j = low;

        for(i=low; i<=high; i++) {
            if(S[i] == pivotitem) {
                temp = S[i];
                S[i] = S[j];
                S[j] = temp;
                mark = j;
                j++;
            }
            else if(S[i] < pivotitem) {
                temp = S[i];
                S[i] = S[j];
                S[j] = temp;
                j++;
            }
        }
        pivotpoint = j-1;
        temp = S[mark];
        S[mark] = S[pivotpoint];
        S[pivotpoint] = temp;
    }
Mark Peters
  • 76,122
  • 14
  • 153
  • 186
user1189352
  • 3,534
  • 9
  • 43
  • 77
  • 1
    What effort have you put into debugging this yourself? – Mark Peters Mar 03 '12 at 14:56
  • 1
    I cannot find the 'ERROR HERE!!!!' section! Did you try using a debugger? – home Mar 03 '12 at 14:56
  • 2
    The debugger is a wonderful tool. Learning to use it is invaluable. – Brian Roach Mar 03 '12 at 14:59
  • the part where i have: T[i] = S[(first+last)/2]; is where I get the error – user1189352 Mar 03 '12 at 14:59
  • 1
    We understand that, have you read any of the comments above? – Hovercraft Full Of Eels Mar 03 '12 at 15:00
  • using Notepad++.. I guess I can download a better compiler.. – user1189352 Mar 03 '12 at 15:00
  • 1
    Or even use `System.out.println(...)` statements as a "poor man's debugger". Just try to do something. – Hovercraft Full Of Eels Mar 03 '12 at 15:00
  • 1
    Hint: Arrays are *zero based*. Look at your loop. Look at the size of `T`, look at the value of `i` – Brian Roach Mar 03 '12 at 15:03
  • thanks Brian, will look at that – user1189352 Mar 03 '12 at 15:04
  • ouch well i see what the values are... but I guess not understanding how the algorithm truly works I wont' be able to fix it – user1189352 Mar 03 '12 at 15:12
  • any other help? I mean I see for an array size of 10, the length of T is 2.. but on the 2nd loop, first and last average out to larger than that. – user1189352 Mar 03 '12 at 15:13
  • @user1189352 - In all seriousness we're not trying to me mean or rude. SO is to help people learn, not solve their problems for them. You are accessing an index that doesn't exist in either `T` or `S`. The way to figure out why is to either instrument the code with `print` statements or use the debugger and look at the all the values that are being used. That's what any of us would have to do. If you've implemented the algorithm incorrectly, this should shed light on it and allow you to then ask a specific question about the algorithm if you're still stuck. – Brian Roach Mar 03 '12 at 15:34
  • i understand. i've been up all night working on this.. and this is the last part i need to finish.. so seriously, i'm probably not seeing things that I normally should be seeing. – user1189352 Mar 03 '12 at 15:35
  • as i commented below though, so you're right, the arrays are 0 based, but changing the i=0 messes up my values for first and last, giving it negative values. and i guess without fully understanding the algorithm, i'm not going to know how to change the values accordingly. – user1189352 Mar 03 '12 at 15:35
  • i'll probably just have to give it another look when my mind is fresh.. i was really hoping i wouldn't have to "understand" it too much to be honest as the assignment is to test and report which algorithms are fastest.. programming the algorithm isn't even worth any points so that's the frustrating part. – user1189352 Mar 03 '12 at 15:37

1 Answers1

1

You allocate T as 0..r-1:

int[] T = new int[r];

Then you step through 1..r

for(i=1; i<=r; i++)
T[i] =
stark
  • 10,399
  • 2
  • 29
  • 44