Here is a function that I wrote to sort small arrays (<1000 elements). When I compared the performance to other examples online (Heap sort and merge sort). I found that mine seemed to perform as well or better. I'm curious what the official name of this type of sort is?
function Sort(arr) {
let out = [];
let Max = 0;
let Min = 0;
let cur = 0;
cur = arr[0];
Max = cur;
Min = Max;
out.push(cur);
for (let i = 1; i < arr.length; i++) {
cur = arr[i];
if (Min == Max) {
if (cur > Max) {
out.unshift(cur);
Max = cur;
}
else {
out.push(cur);
Min = cur;
}
}
else {
if (cur > Max) {
out.unshift(cur);
Max = cur;
}
else {
if (cur < Min) {
out.push(cur);
Min = cur;
}
else {
//splice into the middle
for (let z = 1; z < out.length; z++) {
if (cur > out[z]) {
out.splice(z, 0, cur);
break;
}
}
}
}
}
}
return out;
}
O(N^3), as soon as simplest obvious sorting algorithm isO(N^2), I doubt that somebody decided to give this one nameSort()?