From d6fd7ab672fa1c026e07de7802c6547d20871f93 Mon Sep 17 00:00:00 2001 From: John Reynolds Date: Mon, 11 Oct 2021 04:52:03 -0400 Subject: [PATCH] fix sort blocks overflow + var name change --- .../src/holygrail/HolyGrailSort.java | 20 +++++++++---------- .../Java/SmartBlockSelectSort.java | 20 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Holy Grail Sort/Java/Summer Dragonfly et al.'s Rough Draft/src/holygrail/HolyGrailSort.java b/Holy Grail Sort/Java/Summer Dragonfly et al.'s Rough Draft/src/holygrail/HolyGrailSort.java index c80113c..8905387 100644 --- a/Holy Grail Sort/Java/Summer Dragonfly et al.'s Rough Draft/src/holygrail/HolyGrailSort.java +++ b/Holy Grail Sort/Java/Summer Dragonfly et al.'s Rough Draft/src/holygrail/HolyGrailSort.java @@ -37,7 +37,7 @@ // from the rest of the team! // // Current status: Completely broken, messy, and filled with shortcuts; -// PLEASE DO NOT USE YET (10/2/21) +// PLEASE DO NOT USE YET (10/11/21) /* * The Holy Grail Sort Project @@ -609,17 +609,17 @@ private static void sortBlocks(T[] array, int firstKey, int start, int block // consider anonymous' suggestion int lastKey = firstKey + blockCount - 1; - int scrambledIndex = rightKey < lastKey ? rightKey + 1 : rightKey; + int scrambledEnd = rightKey < lastKey ? rightKey + 1 : rightKey; // phase two: replace the entire left subarray with blocks in sorted order from the // scrambled area, keeping track of the rightmost block swapped - do { + while(keyIndex < rightKey) { int selectBlock = rightBlock; int selectKey = rightKey; int currBlock = rightBlock + blockLen; - for(int currKey = rightKey + 1; currKey <= scrambledIndex; currKey++, currBlock += blockLen) { + for(int currKey = rightKey + 1; currKey <= scrambledEnd; currKey++, currBlock += blockLen) { int compare = cmp.compare(array[currBlock + cmpIndex], array[selectBlock + cmpIndex]); if (compare < 0 || (compare == 0 && cmp.compare(array[ currKey], array[selectKey]) < 0)) { @@ -631,23 +631,23 @@ private static void sortBlocks(T[] array, int firstKey, int start, int block swapBlocksForwards(array, blockIndex, selectBlock, blockLen); swap(array, keyIndex, selectKey); - if(selectKey == scrambledIndex && scrambledIndex < lastKey) scrambledIndex++; + if(selectKey == scrambledEnd && scrambledEnd < lastKey) scrambledEnd++; blockIndex += blockLen; keyIndex++; - } while(keyIndex < rightKey); + } // phase three: after the left subarray has been sorted, keep finding the next block in order // from the scrambled area until either (a) the scrambled area runs out of blocks, // meaning the rest are sorted, or (b) the scrambled area hits the end of the right // subarray - while(scrambledIndex < lastKey) { + while(scrambledEnd < lastKey) { int selectBlock = blockIndex; int selectKey = keyIndex; int currBlock = blockIndex + blockLen; - for(int currKey = keyIndex + 1; currKey <= scrambledIndex; currKey++, currBlock += blockLen) { + for(int currKey = keyIndex + 1; currKey <= scrambledEnd; currKey++, currBlock += blockLen) { int compare = cmp.compare(array[currBlock + cmpIndex], array[selectBlock + cmpIndex]); if (compare < 0 || (compare == 0 && cmp.compare(array[ currKey], array[selectKey]) < 0)) { @@ -660,13 +660,13 @@ private static void sortBlocks(T[] array, int firstKey, int start, int block swapBlocksForwards(array, blockIndex, selectBlock, blockLen); swap(array, keyIndex, selectKey); - if(selectKey == scrambledIndex) scrambledIndex++; + if(selectKey == scrambledEnd) scrambledEnd++; } blockIndex += blockLen; keyIndex++; - if(keyIndex == scrambledIndex) return; + if(keyIndex == scrambledEnd) return; } // phase four: sort the remainder blocks from the scrambled area diff --git a/Research/Block Sorting/Smart Block Selection Sort/Java/SmartBlockSelectSort.java b/Research/Block Sorting/Smart Block Selection Sort/Java/SmartBlockSelectSort.java index aa6d884..2fd020b 100644 --- a/Research/Block Sorting/Smart Block Selection Sort/Java/SmartBlockSelectSort.java +++ b/Research/Block Sorting/Smart Block Selection Sort/Java/SmartBlockSelectSort.java @@ -1,6 +1,6 @@ // implementation of "smart block selection" sort // code inspired by Anonymous0726 -// TODO: CURRENTLY BROKEN; please do not use!! +// TODO: possibly fixed??? private static void sortBlocks(T[] array, int firstKey, int start, int blockCount, int leftBlocks, int blockLen, boolean sortByTail, Comparator cmp) { // this check might be unnecessary with the new "combine blocks" control flow if(blockCount == leftBlocks) return; @@ -32,17 +32,17 @@ private static void sortBlocks(T[] array, int firstKey, int start, int block // consider anonymous' suggestion int lastKey = firstKey + blockCount - 1; - int scrambledIndex = rightKey < lastKey ? rightKey + 1 : rightKey; + int scrambledEnd = rightKey < lastKey ? rightKey + 1 : rightKey; // phase two: replace the entire left subarray with blocks in sorted order from the // scrambled area, keeping track of the rightmost block swapped - do { + while(keyIndex < rightKey) { int selectBlock = rightBlock; int selectKey = rightKey; int currBlock = rightBlock + blockLen; - for(int currKey = rightKey + 1; currKey <= scrambledIndex; currKey++, currBlock += blockLen) { + for(int currKey = rightKey + 1; currKey <= scrambledEnd; currKey++, currBlock += blockLen) { int compare = cmp.compare(array[currBlock + cmpIndex], array[selectBlock + cmpIndex]); if (compare < 0 || (compare == 0 && cmp.compare(array[ currKey], array[selectKey]) < 0)) { @@ -54,23 +54,23 @@ private static void sortBlocks(T[] array, int firstKey, int start, int block swapBlocksForwards(array, blockIndex, selectBlock, blockLen); swap(array, keyIndex, selectKey); - if(selectKey == scrambledIndex && scrambledIndex < lastKey) scrambledIndex++; + if(selectKey == scrambledEnd && scrambledEnd < lastKey) scrambledEnd++; blockIndex += blockLen; keyIndex++; - } while(keyIndex < rightKey); + } // phase three: after the left subarray has been sorted, keep finding the next block in order // from the scrambled area until either (a) the scrambled area runs out of blocks, // meaning the rest are sorted, or (b) the scrambled area hits the end of the right // subarray - while(scrambledIndex < lastKey) { + while(scrambledEnd < lastKey) { int selectBlock = blockIndex; int selectKey = keyIndex; int currBlock = blockIndex + blockLen; - for(int currKey = keyIndex + 1; currKey <= scrambledIndex; currKey++, currBlock += blockLen) { + for(int currKey = keyIndex + 1; currKey <= scrambledEnd; currKey++, currBlock += blockLen) { int compare = cmp.compare(array[currBlock + cmpIndex], array[selectBlock + cmpIndex]); if (compare < 0 || (compare == 0 && cmp.compare(array[ currKey], array[selectKey]) < 0)) { @@ -83,13 +83,13 @@ private static void sortBlocks(T[] array, int firstKey, int start, int block swapBlocksForwards(array, blockIndex, selectBlock, blockLen); swap(array, keyIndex, selectKey); - if(selectKey == scrambledIndex) scrambledIndex++; + if(selectKey == scrambledEnd) scrambledEnd++; } blockIndex += blockLen; keyIndex++; - if(keyIndex == scrambledIndex) return; + if(keyIndex == scrambledEnd) return; } // phase four: sort the remainder blocks from the scrambled area