Skip to content
Snippets Groups Projects
Commit 4dde7241 authored by Lawrence Chung's avatar Lawrence Chung
Browse files

Conflict fix

parents 018a6081 5c7e3d16
No related branches found
No related tags found
No related merge requests found
package sandbox;
public interface GeneralCompare<T> {
public int compare(T a1, T a2);
public interface GeneralCompare {
public int eval(Comparable a1, Comparable a2);
}
package sort;
public interface GeneralCompare<T> {
public int compare(T a1, T a2);
public interface GeneralCompare {
public int compare(Comparable a1, Comparable a2);
}
package sort;
import java.lang.reflect.Array;
public class MergeSort{
private static Comparable[] aux;
public static void merge(Comparable[] x, int lo, int hi, GeneralCompare gc){
public static void main(String[] args) {
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);
int n = hi - lo;
aux = new Comparable[n];
for (int i = 0 ; i < (Array.getLength(test) - 1) ; 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);
}
private static void sortWrapped(Comparable[] x, int lo, int hi, GeneralCompare gc) {
int n = hi - lo;
if(n <= 1)
return;
// Recursively merge each half of the array
int mid = (n/2) + 1;
merge(x, lo, mid, gc);
merge(x, mid+1, hi, gc);
// Recursively sort each half of the array
int mid = lo + (n/2);
sortWrapped(x, lo, mid, gc);
sortWrapped(x, mid+1, hi, gc);
merge(x, lo, hi, gc);
}
private static void merge(Comparable[] x, int lo, int hi, GeneralCompare gc){
int n = hi - lo;
int mid = lo + (n/2);
// Fill auxiliary array
for(int k = 1; k <= n; k++){
System.out.println(n);
for(int k = lo; k <= hi; k++){
aux[k] = x[k];
}
//
while ((mid < hi) | (lo < mid)) {
if (compare(aux[lo], aux[mid]))
x[lo++] = mid;
int i = 0;
// Merging two sorted arrays
while ((lo <= mid) & (mid <= hi)) {
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
for(int k = 1; k <= n; k++){
if(i > j-1)
......@@ -40,9 +64,7 @@ public class MergeSort{
x[k] = aux[j++];
else
x[k] = aux[i++];
}
}
}*/
}
}
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