java - Bug help, Array out of bounds at high values -


i getting out of bounds exception thrown when use median of medians code find given location of k. not sure of exact size @ begins throwing error, code works arrays of size < 75, , error starts when exceed array size 100.

private static int mmquicksortrecursive(integer[] a, int k) {     if (a.length <= 10)     {         arrays.sort(a);         return a[k-1];     }     int n = a.length;     // partition l subsets s[i] of 5 elements each (n/5 subsets).     arraylist<integer[]> list = new arraylist<integer[]>();     int cnt = 0;     int m = n/5;     for( int = 0; < m; i++ ) {         integer[] arr = new integer[5];         for( int j = 0; j < 5; j++ ) {             if( cnt == n )                  break;             arr[j] = a[cnt++];         }         arrays.sort(arr);         list.add(arr);     }     integer[] x = new integer[m];     (int = 0; i< m; i++ ) {         x[i] = list.get(i)[2];     }     int v = x[0];     if(x.length > 2) {                     ///////// error thrown on below line /////////////////         v = (x.length%2 == 0)? x[x.length%2-1]: x[x.length/2];     }     //    partition l 3 parts, l1<m, l2=m, l3>m     integer[] l = partition_left( a, v );     integer[] r = partition_right( a, v );     if( k == l.length+1 ) {         return v;     } else if( k <= l.length ){         return mmquicksortrecursive(l,k);                                    } else {         return mmquicksortrecursive(r,k-l.length-1);                                 }         } 

v = (x.length%2 == 0)? x[x.length%2-1]: x[x.length/2]; 

seems wrong. shouldn't be

v = (x.length%2 == 0)? x[x.length/2]: x[x.length/2-1]; 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -