split array in runs
This commit is contained in:
@ -6,8 +6,9 @@ import de.redstoneunion.git.MrGeorgen.timsort.timsort;
|
|||||||
|
|
||||||
public class test {
|
public class test {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int[] array = {0, 3, -1, -3, -6, -6, 10, 10, 789, 456};
|
int[] array = {0, 3, 4, 8, -6, -6, 10, 10, 789, 456, 0, -1, -2, 8, 10};
|
||||||
int[] output = timsort.insertionsort(array);
|
int[] output = timsort.insertionsort(array);
|
||||||
System.out.println(Arrays.toString(output));
|
//System.out.println(Arrays.toString(output));
|
||||||
|
timsort.timsort(array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,8 @@
|
|||||||
package de.redstoneunion.git.MrGeorgen.timsort;
|
package de.redstoneunion.git.MrGeorgen.timsort;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class timsort {
|
public class timsort {
|
||||||
public static int[] insertionsort(int[] unsorted) {
|
public static int[] insertionsort(int[] unsorted) {
|
||||||
int[] output = new int[unsorted.length];
|
int[] output = new int[unsorted.length];
|
||||||
@ -8,7 +11,6 @@ public class timsort {
|
|||||||
int minIndex = 0;
|
int minIndex = 0;
|
||||||
int maxIndex = i;
|
int maxIndex = i;
|
||||||
for(int j = maxIndex - 1; minIndex != maxIndex; j = minIndex + (maxIndex - minIndex) / 2) {
|
for(int j = maxIndex - 1; minIndex != maxIndex; j = minIndex + (maxIndex - minIndex) / 2) {
|
||||||
System.out.println("min: " + minIndex + " max: " + maxIndex);
|
|
||||||
if(unsorted[i] >= output[j]) {
|
if(unsorted[i] >= output[j]) {
|
||||||
minIndex = j + 1;
|
minIndex = j + 1;
|
||||||
}
|
}
|
||||||
@ -16,7 +18,6 @@ public class timsort {
|
|||||||
maxIndex = j;
|
maxIndex = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println(unsorted[i] + ":" + minIndex);
|
|
||||||
for(int j = i; j > minIndex; --j) {
|
for(int j = i; j > minIndex; --j) {
|
||||||
output[j] = output[j - 1];
|
output[j] = output[j - 1];
|
||||||
}
|
}
|
||||||
@ -24,4 +25,32 @@ public class timsort {
|
|||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
public static int[] timsort(int[] unsorted) {
|
||||||
|
int r = 0;
|
||||||
|
// r becomes 1 any 1 bits are shifted off
|
||||||
|
int n = 16;
|
||||||
|
while(n >= 64) {
|
||||||
|
r |= (n & 1);
|
||||||
|
n >>= 1;
|
||||||
|
}
|
||||||
|
int minRun = n + r;
|
||||||
|
ArrayList<int[]> runs = new ArrayList<int[]>();
|
||||||
|
for(int i = 0; i < unsorted.length - 1;) {
|
||||||
|
int j = i;
|
||||||
|
boolean ascending = true;
|
||||||
|
boolean descending = true;
|
||||||
|
for(; i < unsorted.length - 1 && (ascending || descending); ++i) {
|
||||||
|
ascending = ascending && unsorted[i] <= unsorted[i + 1];
|
||||||
|
descending = descending && unsorted[i] >= unsorted[i + 1];
|
||||||
|
}
|
||||||
|
if(i - j < minRun) i = j + minRun;
|
||||||
|
if(i >= unsorted.length - 1) i = unsorted.length;
|
||||||
|
runs.add(Arrays.copyOfRange(unsorted, j, i));
|
||||||
|
}
|
||||||
|
int[][] notMerged= new int[runs.size()][];
|
||||||
|
for(int i = 0; i < runs.size(); ++i) {
|
||||||
|
notMerged[i] = insertionsort(runs.get(i));
|
||||||
|
}
|
||||||
|
return new int[1];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user