1 Replies - 252 Views - Last Post: 19 February 2010 - 06:37 PM Rate Topic: -----

#1 ssrhhrm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-February 10

arrayindexoutofboundsexception

Posted 19 February 2010 - 06:30 PM

Hello,
I am writing code for quicksort but get an arrayindexoutofbounds exception. I am new to java and need help figuring out what is causing it and how to fix it.

My main
Integer[] A ;
    System.out.println("    LENGTH   SELECTION       MERGE       QUICK        JAVA") ;
    for (int length = startLength ; true ; length += incrementLength) {
      System.out.printf("%10d", length ) ;
      A = makeRandomArray(length,maximumElement) ;
      startTimer() ;
      SortingMethods.selectionSort(A) ;
      System.out.printf("%12d", getElapsedTime() ) ;
      A = makeRandomArray(length,maximumElement) ;
      startTimer() ;
      SortingMethods.mergeSort(A, A[0], A[A.length-1]) ;
      System.out.printf("%12d", getElapsedTime() ) ;
      A = makeRandomArray(length,maximumElement) ;
      startTimer() ;
      SortingMethods.quickSort(A, A[0], A[A.length-1]) ;
      System.out.printf("%12d", getElapsedTime() ) ;
      A = makeRandomArray(length,maximumElement) ;
      startTimer() ;
      Arrays.sort(A) ;
      System.out.printf("%12d", getElapsedTime() ) ;
      System.out.println() ;
      System.out.println() ;
    }
  }


And my quicksort is as follows:
public static <T extends Comparable<T>> void quickSort(T[] A, int left, int right) // quick sort
         {
        	 if (right <= left) return;
             int i = partition(A, left, right);
             quickSort(A, left, i-1);
             quickSort(A, i+1, right);  
                 
         }
                        
         private static <T extends Comparable<T>> int partition(T[] a, int left, int right) {
             int i = left - 1;
             int j = right;
             while (true) {
                 while (less(a, a[++i], a[right]));     // find item on left to swap
                                                     // a[right] acts as sentinel
                 while (less(a, a[right], a[--j]))      // find item on right to swap
                     if (j == left) break;           // don't go out-of-bounds
                 if (i >= j) break;                  // check if pointers cross
                 exch(a, i, j);                      // swap two elements into place
             }
             exch(a, i, right);                      // swap with partition element
             return i;
         }
         
      // is x < y ?
         private static <T extends Comparable<T>> boolean less(T[] a, T x, T y) {
             return (true);
         }

         // exchange a[i] and a[j]
         private static <T extends Comparable<T>> void exch(T[] a, int i, int j) {
             T swap = a[i];
             a[i] = a[j];
             a[j] = swap;
         }



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: arrayindexoutofboundsexception

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9042
  • View blog
  • Posts: 33,544
  • Joined: 27-December 08

Re: arrayindexoutofboundsexception

Posted 19 February 2010 - 06:37 PM

The reason you get an IndexOutOfBoundsException is b/c the parameters you are passing, A[0] and A[A.length-1], should really be indices (0 and A.length-1), not the elements. Because if A[0] happens to be 100 and A.length is only 10, the partition() and sort() methods (which assume these params are indices and treats them as such) will end up giving you index out of bounds exceptions.
SortingMethods.quickSort(A, A[0], A[A.length-1]) ; 


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1