Skip to content
Snippets Groups Projects
Commit 0354358c authored by Haley Glavina's avatar Haley Glavina
Browse files

Introduced Null Pointer Exception at lambda function, sorts two array

parent 5c7e3d16
No related branches found
No related tags found
No related merge requests found
......@@ -10,21 +10,22 @@ public class MergeSort{
GeneralCompare b1;
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
Integer[] test = {3, 4, 2, 1, 5, 7, 9, 10};
sort(test, 0, Array.getLength(test) - 1, b1);
//Integer[] test = {2, 1};
sort(test, 0, test.length - 1, b1);
for (int i = 0 ; i < (Array.getLength(test) - 1) ; i++) {
for (int i = 0 ; i < (test.length) ; i++) {
System.out.println(test[i]);
}
}
public static void sort(Comparable[] x, int lo, int hi, GeneralCompare gc) {
aux = new Comparable[Array.getLength(x)];
sortWrapped(x, 0, Array.getLength(x) - 1, gc);
aux = new Comparable[x.length];
sortWrapped(x, lo, hi, gc);
}
private static void sortWrapped(Comparable[] x, int lo, int hi, GeneralCompare gc) {
int n = hi - lo;
if(n <= 1)
if(n < 1)
return;
// Recursively sort each half of the array
int mid = lo + (n/2);
......@@ -39,19 +40,36 @@ public class MergeSort{
int mid = lo + (n/2);
// Fill auxiliary array
System.out.println(n);
System.out.println("lo, mid, hi: " + lo + ", " + mid + ", " + hi);
for(int k = lo; k <= hi; k++){
aux[k] = x[k];
}
int i = lo;
int j = mid + 1;
for (int k = 0 ; k <= hi ;) {
if (i > mid)
x[k++] = aux[j++]; //All elems in first half already added to x
else if (j > hi)
x[k++] = aux[i++]; //All elems in second half already added to x
else if (gc.compare(aux[i], aux[j]) > 0)
x[k++] = aux[j++];
else
x[k++] = aux[i++];
}
/*
int i = 0;
// Merging two sorted arrays
while ((lo <= mid) & (mid <= hi)) {
while (i <= n) {
if (gc.compare(aux[lo], aux[mid]) > 0)
x[i++] = aux[mid++];
else
x[i++] = aux[lo++];
}
*/
/*
// Is this portion sorting the aux array? Check
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment