diff --git a/src/main/java/de/redstoneunion/git/MrGeorgen/timsort/timsort.java b/src/main/java/de/redstoneunion/git/MrGeorgen/timsort/timsort.java index ad39726..6c172e6 100644 --- a/src/main/java/de/redstoneunion/git/MrGeorgen/timsort/timsort.java +++ b/src/main/java/de/redstoneunion/git/MrGeorgen/timsort/timsort.java @@ -8,23 +8,25 @@ public class timsort { int[] output = new int[unsorted.length]; output[0] = unsorted[0]; for(int i = 1; i < unsorted.length; ++i) { - int minIndex = 0; - int maxIndex = i; - for(int j = maxIndex - 1; minIndex != maxIndex; j = minIndex + (maxIndex - minIndex) / 2) { - if(unsorted[i] >= output[j]) { - minIndex = j + 1; - } - else { - maxIndex = j; - } - } - for(int j = i; j > minIndex; --j) { + int foundIndex = binarySearch(output, unsorted[i], 0, i); + for(int j = i; j > foundIndex; --j) { output[j] = output[j - 1]; } - output[minIndex] = unsorted[i]; + output[foundIndex] = unsorted[i]; } return output; } + private static int binarySearch(int[] array, int search, int minIndex, int maxIndex) { + for(int j = maxIndex - 1; minIndex != maxIndex; j = minIndex + (maxIndex - minIndex) / 2) { + if(search >= array[j]) { + minIndex = j + 1; + } + else { + maxIndex = j; + } + } + return minIndex; + } public static int[] timsort(int[] unsorted) { int r = 0; // r becomes 1 any 1 bits are shifted off @@ -79,11 +81,21 @@ public class timsort { int[] j = new int[2]; int w = 0; int l = 0; - for(; j[w] < notMerged[i + w].length; ++l) { + for(int gallopingCounter = 0; j[w] < notMerged[i + w].length; ++l) { + if(gallopingCounter == 7) { + int b = w != 0 ? 0 : 1; + int found = binarySearch(notMerged[i + w], notMerged[i + b][j[b]], j[w], notMerged[i + w].length - 1); + for(; j[w] < found; ++j[w], ++l) { + temp[l] = notMerged[i + w][j[w]]; + } + } + int lastW = w; w = notMerged[i][j[0]] > notMerged[i + 1][j[1]] ? 1 : 0; System.out.println("w: " + w); temp[l] = notMerged[i + w][j[w]]; ++j[w]; + if(w == lastW) ++gallopingCounter; + else gallopingCounter = 0; } int b = w != 0 ? 0 : 1; for(int k = j[b]; l < temp.length; ++k, ++l) {