sorting - I have an issue with implementing Merge Sort for Java -
public static void main(string[] args) { int[] test = {5,4,3,5,7,5,1,5,96}; system.out.print("before: "); printlist(test); mergesort(test, 1, test.length); //system.out.print("after: "); //printlist(test); } public static void printlist(int[] test){ (int i= 0; < test.length; i++){ system.out.print(test[i] + " "); } system.out.println(); } public static void merge(int[] a, int p, int q, int r){ int n1 = q - p + 1; int n2 = r - q; int[] l = new int[n1]; int[] r = new int[n2]; for(int = 1; <= n1; i++){ l[i] = a[p+i-1]; } (int j = 1; j <= n2; j++){ r[j] = a[q+j]; } int = 1; int j = 1; (int k=p; <= r; i++){ if (i > n1){ a[k] = r[j]; j++; } else if (j > n2){ a[k] = l[i]; i++; } else if (l[i] <= r[j]){ a[k] = l[i]; i++; } else{ a[k] = r[j]; j++; } } } public static void mergesort(int[] a, int p, int r){ if (p < r){ int q = (p + r) / 2; mergesort(a, p, q); mergesort(a, q+1, r); merge(a, p, q, r); } }
i trying implement merge sort on test array, not sure why getting arrayindexoutofboundsexception error on this. assignment change merge sort code not use sentinels when searching.
before: 5 4 3 5 7 5 1 5 96 exception in thread "main" java.lang.arrayindexoutofboundsexception: 1 @ lab1_2.merge(lab1_2.java:28) @ lab1_2.mergesort(lab1_2.java:61) @ lab1_2.mergesort(lab1_2.java:59) @ lab1_2.mergesort(lab1_2.java:59) @ lab1_2.mergesort(lab1_2.java:59) @ lab1_2.main(lab1_2.java:8)
this error message get.
you getting arrayindexoutofboundsexception run time exception because try accessing array out of array boundary (limits). in merge method statements
int[] l = new int[n1];
you declare array of size n1 can element @ index 0 n-1. try storing element @ index n1. not possible because know array having element 0 size-1 (here size length of array) 1 of reason. have issue other places. edit code , hope following code work you.
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; class ideone { public static void main (string[] args) throws java.lang.exception { // code goes here int[] test = {5,4,3,5,7,5,1,5,96}; system.out.print("before: "); printlist(test); mergesort(test, 0, test.length-1); system.out.print("after: "); printlist(test); } public static void printlist(int[] test){ (int i= 0; < test.length; i++){ system.out.print(test[i] + " "); } system.out.println(); } public static void merge(int[] a, int p, int q, int r){ int n1 = q - p + 1; int n2 = r - q; int[] l = new int[n1]; int[] r = new int[n2]; for(int = 0; < n1; i++){ l[i] = a[p+i]; } (int j = 0; j < n2; j++){ r[j] = a[q+j+1]; } //int = 0; //int j = 0; /* (int k=p; <= r; i++){ if (i > n1){ a[k] = r[j]; j++; } else if (j > n2){ a[k] = l[i]; i++; } else if (l[i] <= r[j]){ a[k] = l[i]; i++; } else{ a[k] = r[j]; j++; } }*/ int = 0, j = 0; int k = p; while (i < n1 && j < n2) { if (l[i] <= r[j]) { a[k] = l[i]; i++; } else { a[k] = r[j]; j++; } k++; } while (i < n1) { a[k] = l[i]; i++; k++; } while (j < n2) { a[k] = r[j]; j++; k++; } } public static void mergesort(int[] a, int p, int r){ if (p < r){ int q = (p + r) / 2; mergesort(a, p, q); mergesort(a, q+1, r); merge(a, p, q, r); } } }
Comments
Post a Comment