diff --git a/src/sort/KDT.java b/src/sort/KDT.java index cb6b9bc21953ff04f2cfba118aab87cec794d21e..681deb7511a90d6832883b295c8e842537cf5c08 100644 --- a/src/sort/KDT.java +++ b/src/sort/KDT.java @@ -119,7 +119,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> implements Serializable { int axis = depth % getK(); int mid = (lo + hi) / 2; - MergeSort.sort(keyvals, lo, hi, axes.get(axis)); + QuickSelect.median(keyvals, lo, hi, axes.get(axis)); KeyVal median = (KeyVal) keyvals[mid]; //TODO: fix size diff --git a/src/sort/QuickSelect.java b/src/sort/QuickSelect.java index 623352224c0c0e228cba2167722fedb2b5d63e87..1b7e0e37e777a5587eea63896935e63131df550c 100644 --- a/src/sort/QuickSelect.java +++ b/src/sort/QuickSelect.java @@ -30,6 +30,16 @@ public class QuickSelect { sort(a, 0, a.length - 1, a.length/2, gc); } + /** + * Partially sorts a comparable array such that elements smaller than the median occur in + * the first half of the array, and elements larger than the median occur in the second half + * @param a Array of comparable items + * @param gc Lambda function to compare items + */ + public static <T> void median(Comparable<T>[] a, int lo, int hi, GeneralCompare<T> gc) { + sort(a, lo, hi, (hi - lo) / 2, gc); + } + /** * Partially sorts a comparable array such that elements smaller than the kth largest element * occur in the first half of the array, and larger elements occur in the second half