-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
much improved key sorting for strategy 2, improved shell sort, update…
… research folder also: - slightly modify control's key sort for consistency with aphitorite's new key sort - fix bug with shell sort
- Loading branch information
1 parent
0fb10c2
commit b367919
Showing
3 changed files
with
138 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ing/aphitorite's Novel Bufferless Key Sort for Strategy 2/Java/SortKeysWithoutBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
private static <T> void groupKeys(T[] array, int left, int right, T medianKey, Comparator<T> cmp) { | ||
while(left < right && cmp.compare(array[left], medianKey) < 0) left++; | ||
|
||
for(int i = left + 1; i < right; i++) { | ||
if(cmp.compare(array[i], medianKey) < 0) { | ||
insertBackwards(array, left, i - left); | ||
left++; | ||
} | ||
} | ||
} | ||
|
||
private static <T> void mergeGroups(T[] array, int left, int middle, int right, T medianKey, Comparator<T> cmp) { | ||
int leftLen = middle - left; | ||
int rightLen = right - middle; | ||
|
||
int mergeStart = left + binarySearchLeft(array, left, leftLen, medianKey, cmp); | ||
int mergeLen = binarySearchLeft(array, middle, rightLen, medianKey, cmp); | ||
|
||
rotate(array, mergeStart, middle - mergeStart, mergeLen); | ||
} | ||
|
||
// another novel, elegant, and efficient key sort for Holy Grailsort, specifically for Strategy 2 | ||
// based on lazy stable sorting, this algorithm achieves less than 2n comparisons and O(n log n) moves | ||
// works based off of the same assumptions as 'sortKeys' | ||
// designed and implemented by aphitorite | ||
private static <T> void lazySortKeys(T[] array, int firstKey, int keyCount, T medianKey, Comparator<T> cmp) { | ||
int runLen = 8; | ||
int keysEnd = firstKey + keyCount; | ||
|
||
int i; | ||
for(i = firstKey; i + runLen < keysEnd; i += runLen) { | ||
groupKeys(array, i, i + runLen, medianKey, cmp); | ||
} | ||
groupKeys(array, i, keysEnd, medianKey, cmp); | ||
|
||
while(runLen < keyCount) { | ||
int fullMerge = 2 * runLen; | ||
|
||
int mergeIndex; | ||
int mergeEnd = keysEnd - fullMerge; | ||
|
||
for(mergeIndex = firstKey; mergeIndex <= mergeEnd; mergeIndex += fullMerge) { | ||
mergeGroups(array, mergeIndex, mergeIndex + runLen, mergeIndex + fullMerge, medianKey, cmp); | ||
} | ||
|
||
int leftOver = keysEnd - mergeIndex; | ||
if(leftOver > runLen) { | ||
mergeGroups(array, mergeIndex, mergeIndex + runLen, keysEnd, medianKey, cmp); | ||
} | ||
|
||
runLen *= 2; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...rch/Key Sorting/aphitorite's Novel Bufferless Key Sort for Strategy 2/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |