almost working
This commit is contained in:
@ -6,9 +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, 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);
|
int[] output = timsort.insertionsort(array);
|
||||||
//System.out.println(Arrays.toString(output));
|
System.out.println("Insertionsort test: " + Arrays.toString(output));
|
||||||
timsort.timsort(array);
|
System.out.println("End result: " + Arrays.toString(timsort.timsort(array)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,8 +28,8 @@ public class timsort {
|
|||||||
public static int[] timsort(int[] unsorted) {
|
public static int[] timsort(int[] unsorted) {
|
||||||
int r = 0;
|
int r = 0;
|
||||||
// r becomes 1 any 1 bits are shifted off
|
// r becomes 1 any 1 bits are shifted off
|
||||||
int n = 16;
|
int n = unsorted.length;
|
||||||
while(n >= 64) {
|
while(n >= 16) {
|
||||||
r |= (n & 1);
|
r |= (n & 1);
|
||||||
n >>= 1;
|
n >>= 1;
|
||||||
}
|
}
|
||||||
@ -64,8 +64,34 @@ public class timsort {
|
|||||||
}
|
}
|
||||||
int[][] notMerged = new int[runs.size()][];
|
int[][] notMerged = new int[runs.size()][];
|
||||||
for(int i = 0; i < runs.size(); ++i) {
|
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));
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user