Skip to content

Commit

Permalink
update research folder with new key sort
Browse files Browse the repository at this point in the history
  • Loading branch information
MusicTheorist committed Oct 23, 2021
1 parent 706a7c2 commit 0fb10c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// a novel, elegant, and incredibly efficient 1.5 sqrt n key sort, courtesy of Control
// only works if a) median key is known and b) keys were permutated by "sortBlocks"
// (keys < medianKey and >= medianKey are each in relative sorted order, resembling a final radix sort pass)
private static <T> void sortKeys(T[] array, int firstKey, T medianKey, int keyCount, int buffer, Comparator<T> cmp) {
int currKey = firstKey;
int bufferSwaps = 0;

while(currKey < firstKey + keyCount) {
if(cmp.compare(array[currKey], medianKey) < 0) {
if(bufferSwaps != 0) {
swap(array, currKey, currKey - bufferSwaps);
}
}
else {
swap(array, currKey, buffer + bufferSwaps);
bufferSwaps++;
}
currKey++;
}

swapBlocksBackwards(array, currKey - bufferSwaps, buffer, bufferSwaps);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# holy-grail-sort
A faster implementation of in-place, stable, worst-case O(n log n) sorting

0 comments on commit 0fb10c2

Please sign in to comment.