galloping
This commit is contained in:
@ -8,22 +8,24 @@ public class timsort {
|
|||||||
int[] output = new int[unsorted.length];
|
int[] output = new int[unsorted.length];
|
||||||
output[0] = unsorted[0];
|
output[0] = unsorted[0];
|
||||||
for(int i = 1; i < unsorted.length; ++i) {
|
for(int i = 1; i < unsorted.length; ++i) {
|
||||||
int minIndex = 0;
|
int foundIndex = binarySearch(output, unsorted[i], 0, i);
|
||||||
int maxIndex = i;
|
for(int j = i; j > foundIndex; --j) {
|
||||||
|
output[j] = output[j - 1];
|
||||||
|
}
|
||||||
|
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) {
|
for(int j = maxIndex - 1; minIndex != maxIndex; j = minIndex + (maxIndex - minIndex) / 2) {
|
||||||
if(unsorted[i] >= output[j]) {
|
if(search >= array[j]) {
|
||||||
minIndex = j + 1;
|
minIndex = j + 1;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
maxIndex = j;
|
maxIndex = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(int j = i; j > minIndex; --j) {
|
return minIndex;
|
||||||
output[j] = output[j - 1];
|
|
||||||
}
|
|
||||||
output[minIndex] = unsorted[i];
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
public static int[] timsort(int[] unsorted) {
|
public static int[] timsort(int[] unsorted) {
|
||||||
int r = 0;
|
int r = 0;
|
||||||
@ -79,11 +81,21 @@ public class timsort {
|
|||||||
int[] j = new int[2];
|
int[] j = new int[2];
|
||||||
int w = 0;
|
int w = 0;
|
||||||
int l = 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;
|
w = notMerged[i][j[0]] > notMerged[i + 1][j[1]] ? 1 : 0;
|
||||||
System.out.println("w: " + w);
|
System.out.println("w: " + w);
|
||||||
temp[l] = notMerged[i + w][j[w]];
|
temp[l] = notMerged[i + w][j[w]];
|
||||||
++j[w];
|
++j[w];
|
||||||
|
if(w == lastW) ++gallopingCounter;
|
||||||
|
else gallopingCounter = 0;
|
||||||
}
|
}
|
||||||
int b = w != 0 ? 0 : 1;
|
int b = w != 0 ? 0 : 1;
|
||||||
for(int k = j[b]; l < temp.length; ++k, ++l) {
|
for(int k = j[b]; l < temp.length; ++k, ++l) {
|
||||||
|
|||||||
Reference in New Issue
Block a user