Skip to content

Commit

Permalink
add Base-N Max Heap Sort
Browse files Browse the repository at this point in the history
Co-Authored-By: fluffyyboii <[email protected]>
  • Loading branch information
Gaming32 and fluffyyboii committed Nov 23, 2020
1 parent e11852c commit 6e682ee
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sorts/hybrid/BaseNMergeSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public BaseNMergeSort(ArrayVisualizer arrayVisualizer) {
this.setRunSortName("Base-N Mergesort");
this.setCategory("Hybrid Sorts");
this.setComparisonBased(true);
this.setBucketSort(false);
this.setBucketSort(true);
this.setRadixSort(false);
this.setUnreasonablySlow(false);
this.setUnreasonableLimit(0);
Expand Down
47 changes: 47 additions & 0 deletions src/sorts/select/BaseNMaxHeapSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sorts.select;

import main.ArrayVisualizer;
import sorts.templates.Sort;

final public class BaseNMaxHeapSort extends Sort {
public BaseNMaxHeapSort(ArrayVisualizer arrayVisualizer) {
super(arrayVisualizer);
this.setSortListName("Base-N Max Heap");
this.setRunAllSortsName("Base-N Max Heap Sort");
this.setRunSortName("Base-N Max Heapsort");
this.setCategory("Selection Sorts");
this.setComparisonBased(true);
this.setBucketSort(true);
this.setRadixSort(false);
this.setUnreasonablySlow(false);
this.setUnreasonableLimit(0);
this.setBogoSort(false);
}

private void siftDown(int[] arr, int base, int node, int stop) {
int left = node * base + 1;
if (left < stop) {
int maxIndex = left;
for (int i = left + 1; i < left + base; i++) {
if (i >= stop)
break;
if (Reads.compareValues(arr[maxIndex], arr[i]) == -1)
maxIndex = i;
}
if (Reads.compareValues(arr[node], arr[maxIndex]) == -1) {
Writes.swap(arr, node, maxIndex, 1, true, false);
this.siftDown(arr, base, maxIndex, stop);
}
}
}

@Override
public void runSort(int[] arr, int length, int base) {
for (int i = length - 1; i > -1; i--)
this.siftDown(arr, base, i, length);
for (int i = length - 1; i > 0; i--) {
Writes.swap(arr, 0, i, 1, true, false);
this.siftDown(arr, base, 0, i);
}
}
}
8 changes: 8 additions & 0 deletions src/threads/RunComparisonSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public void run() {
extra = 4;
}
}
else if (sort.getRunSortName().equals("Base-N Max Heapsort")) {
try {
extra = getCustomInput("Enter the base for this sort:");
}
catch(Exception e) {
extra = 4;
}
}

arrayVisualizer.setHeading(sort.getRunSortName());
arrayVisualizer.setCategory(sort.getCategory());
Expand Down

0 comments on commit 6e682ee

Please sign in to comment.