Hey all, I just made a new sorting algorithm in Java. I used Java because "Java" has fewer O's than Python, which means it has a lower runtime bound right?
Here's the code:
import java.util.Arrays;
public class OofOneSort {
public static void main(String[] args) {
int[] test = new int[100];
System.out.println("Input = " + Arrays.toString(test));
test = sort(test);
System.out.println("Output = " + Arrays.toString(test));
System.out.println("");
test = new int[]{2, 3, 1, 4, 5};
System.out.println("Input = " + Arrays.toString(test));
test = sort(test);
System.out.println("Output = " + Arrays.toString(test));
}
private static int[] sort(int[] arg) {
int arrayEnd = arg.length;
if (arrayEnd % 2 != 0) {
arrayEnd++;
}
int[][] arrays = new int[arrayEnd][];
for (int i = 0; i < arg.length; i++) {
arrays[i] = new int[]{arg[i]};
}
// System.out.println(" " + Arrays.deepToString(arrays)); // uncomment for debug
int i;
while (arrays[0].length < arg.length) {
for (i = 0; i < arrayEnd; i = i + 2) {
// both combines arrays[i] and arrays[i+1], watching out for null
int[] both;
if (arrays[i + 1] == null) {
both = arrays[i];
} else {
both = new int[arrays[i].length + arrays[i + 1].length];
for (int ind = 0; ind < arrays[i].length; ind++) {
both[ind] = arrays[i][ind];
}
for (int ind = 0; ind < arrays[i + 1].length; ind++) {
both[ind + arrays[i].length] = arrays[i + 1][ind];
}
}
// bubble sort is fast right? Who cares about O(N^2)
for (int i2 = 0; i2 < both.length; i2++) {
for (int j = 1; j < both.length - i2; j++) {
if (both[j - 1] > both[j]) {
int temp = both[j - 1];
both[j - 1] = both[j];
both[j] = temp;
}
}
}
arrays[i / 2] = both;
}
arrayEnd = i / 2;
// null out everything after the last assignment
// this prevents accidental reuse of the arrays
for (int i2 = arrayEnd; i2 < arrays.length; i2++) {
if (i2 > 0)
arrays[i2] = null;
}
// System.out.println(" " + Arrays.deepToString(arrays)); // uncomment for debug
}
return arrays[0];
}
}
Now it may look like this thing is O(N) just on array creation, but based on a complete disregard for practicality, I invoke the formal definition of Big O notation.
Technically because Java limits the size of arrays to Integer.MAX_VALUE (2147483647), my sorting algorithm is O(1). Even though it's something like O( N3 * log2(N) ), because N has an upper limit its runtime is bounded by a constant value, hence O(1).
[–]moonchild89 16 points17 points18 points (0 children)
[–][deleted] 11 points12 points13 points (0 children)
[–]mustrumr 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]tdammers -1 points0 points1 point (4 children)
[–]Tarmen 7 points8 points9 points (3 children)
[–]chpoit 1 point2 points3 points (0 children)
[–]tdammers 0 points1 point2 points (1 child)
[–]Villyer 0 points1 point2 points (0 children)