almost working

This commit is contained in:
2020-12-31 17:44:56 +01:00
parent 94808c7cc6
commit 7d79ee3ec9
2 changed files with 32 additions and 6 deletions

View File

@ -6,9 +6,9 @@ import de.redstoneunion.git.MrGeorgen.timsort.timsort;
public class test {
public static void main(String[] args) {
int[] array = {0, 3, 4, 8, -6, -6, 10, 10, 789, 456, 0, -1, -2, 8, 10};
int[] array = {0, 3, 4, 8, -6, -6, 10, 10, 789, 456, 0, -1, -2, 8, 10, 1, 100, -100, 700, 800, 5};
int[] output = timsort.insertionsort(array);
//System.out.println(Arrays.toString(output));
timsort.timsort(array);
System.out.println("Insertionsort test: " + Arrays.toString(output));
System.out.println("End result: " + Arrays.toString(timsort.timsort(array)));
}
}

View File

@ -28,8 +28,8 @@ public class timsort {
public static int[] timsort(int[] unsorted) {
int r = 0;
// r becomes 1 any 1 bits are shifted off
int n = 16;
while(n >= 64) {
int n = unsorted.length;
while(n >= 16) {
r |= (n & 1);
n >>= 1;
}
@ -64,8 +64,34 @@ public class timsort {
}
int[][] notMerged = new int[runs.size()][];
for(int i = 0; i < runs.size(); ++i) {
System.out.println("Runs before insertionsort: " + Arrays.toString(runs.get(i)));
notMerged[i] = insertionsort(runs.get(i));
System.out.println("After insertionsort: " + Arrays.toString(notMerged[i]));
}
return new int[1];
return merge(notMerged)[0];
}
private static int[][] merge(int[][] notMerged) {
int notEvenLen = notMerged.length % 2;
int[][] merged = new int[notMerged.length / 2 + notEvenLen][];
for(int i = 0; i < notMerged.length - 1; i += 2) {
int[] temp = new int[notMerged[i].length + notMerged[i + 1].length];
merged[i / 2] = temp;
int[] j = new int[2];
for(int l = 0; l < temp.length; ++l) {
int 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(j[w] >= notMerged[i + w].length) {
int b = w != 0 ? 0 : 1;
for(int k = j[b]; k < notMerged[i + b].length; ++k, ++l) {
temp[l] = notMerged[i + b][k];
}
}
}
}
if(notEvenLen == 1) merged[merged.length - 1] = notMerged[notMerged.length - 1];
if(merged.length == 1) return merged;
return merge(merged);
}
}