Skip to content

Commit c684de5

Browse files
author
Oskar Widmark
committed
feat: add child count to heap sort
1 parent 4c1624f commit c684de5

File tree

4 files changed

+50
-42
lines changed

4 files changed

+50
-42
lines changed

src/Options.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const getAlgorithmOptionFields = (
2424
case SortName.CombSort:
2525
return ['shrinkFactor'];
2626
case SortName.Heapsort:
27-
return ['heapType'];
27+
return ['heapType', 'childCount'];
2828
default:
2929
return [];
3030
}
@@ -36,6 +36,7 @@ const isValidOption = (
3636
): value is AlgorithmOptions[typeof field] => {
3737
switch (field) {
3838
case 'base':
39+
case 'childCount':
3940
return Number(value) >= 2 && Number.isInteger(Number(value));
4041
case 'shrinkFactor':
4142
return Number(value) > 1;
@@ -50,6 +51,7 @@ const ALGORITHM_OPTION_LABELS: Record<keyof AlgorithmOptions, string> = {
5051
shrinkFactor: 'Shrink Factor',
5152
heapType: 'Heap Type',
5253
parallel: 'Run in parallel',
54+
childCount: '# of children',
5355
};
5456

5557
const ALGORITHM_OPTION_TEXT_FIELD_TYPES: Record<
@@ -61,6 +63,7 @@ const ALGORITHM_OPTION_TEXT_FIELD_TYPES: Record<
6163
shrinkFactor: 'number',
6264
heapType: 'select',
6365
parallel: 'checkbox',
66+
childCount: 'number',
6467
};
6568

6669
const ALGORITHM_OPTION_VALUES: Record<
@@ -72,6 +75,7 @@ const ALGORITHM_OPTION_VALUES: Record<
7275
shrinkFactor: [],
7376
heapType: ['max', 'min'],
7477
parallel: [],
78+
childCount: [],
7579
};
7680

7781
const ALGORITHM_OPTION_VALUE_LABELS: Record<
@@ -130,7 +134,7 @@ export function Options({
130134
>
131135
Options
132136
</Typography>
133-
<Grid2 container>
137+
<Grid2 container spacing={2}>
134138
{algorithmOptionFields.map((field) => (
135139
<OptionField
136140
key={field}
@@ -153,7 +157,7 @@ const OptionField = (props: {
153157
switch (ALGORITHM_OPTION_TEXT_FIELD_TYPES[field]) {
154158
case 'checkbox':
155159
return (
156-
<FormControl key={field} component="fieldset">
160+
<FormControl key={field} component="fieldset" sx={{ mt: -1 }}>
157161
<LabeledCheckbox
158162
label={ALGORITHM_OPTION_LABELS[field]}
159163
checked={Boolean(nonValidatedOptions[field])}

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export const INIT_SETTINGS: Settings = {
3838
shrinkFactor: 1.3,
3939
heapType: 'max',
4040
parallel: false,
41+
childCount: 2,
4142
},
4243
colorPreset: ColorPreset.Rainbow,
4344
columnColor1: '#ffffff',

src/sorting-algorithms.ts

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -837,74 +837,76 @@ public async mergeSort(arr, start, end){
837837
public async heapsort(arr: SortValue[], options: AlgorithmOptions) {
838838
switch (options.heapType) {
839839
case 'min':
840-
return await this.minHeapsort(arr);
840+
return await this.minHeapsort(arr, options.childCount);
841841
case 'max':
842-
return await this.maxHeapsort(arr);
842+
return await this.maxHeapsort(arr, options.childCount);
843843
}
844844
}
845845

846-
private async minHeapsort(arr: SortValue[]) {
846+
private async minHeapsort(arr: SortValue[], childCount: number) {
847847
for (let i = Math.ceil(arr.length / 2) + 1; i < arr.length; i++) {
848-
await this.minHeapify(arr, 0, i);
848+
await this.minHeapify(arr, 0, i, childCount);
849849
}
850850
for (let i = 0; i < arr.length; i++) {
851851
await this.context.drawAndSwap(arr, arr.length - 1, i);
852-
await this.minHeapify(arr, i, arr.length - 1);
852+
await this.minHeapify(arr, i, arr.length - 1, childCount);
853853
}
854854
}
855855

856-
private async maxHeapsort(arr: SortValue[]) {
856+
private async maxHeapsort(arr: SortValue[], childCount: number) {
857857
for (let i = Math.floor(arr.length / 2) - 1; i >= 0; i--) {
858-
await this.maxHeapify(arr, arr.length, i);
858+
await this.maxHeapify(arr, arr.length, i, childCount);
859859
}
860860
for (let i = arr.length - 1; i > 0; i--) {
861861
await this.context.drawAndSwap(arr, 0, i);
862-
await this.maxHeapify(arr, i, 0);
862+
await this.maxHeapify(arr, i, 0, childCount);
863863
}
864864
}
865865

866866
// This a reversed heap, with the smallest element at the last index
867-
private async minHeapify(arr: SortValue[], n: number, i: number) {
867+
private async minHeapify(
868+
arr: SortValue[],
869+
n: number,
870+
i: number,
871+
childCount: number,
872+
) {
868873
let smallestIndex = i;
869-
const left = arr.length - 1 - (2 * (arr.length - 1 - i) + 2);
870-
const right = arr.length - 1 - (2 * (arr.length - 1 - i) + 1);
871-
if (
872-
left > n &&
873-
(await this.context.compare(arr, smallestIndex, '>', left))
874-
) {
875-
smallestIndex = left;
876-
}
877-
if (
878-
right > n &&
879-
(await this.context.compare(arr, smallestIndex, '>', right))
880-
) {
881-
smallestIndex = right;
874+
for (let c = 0; c < childCount; c++) {
875+
const childIndex =
876+
arr.length - 1 - (childCount * (arr.length - 1 - i) + 1 + c);
877+
if (
878+
childIndex > n &&
879+
(await this.context.compare(arr, childIndex, '<', smallestIndex))
880+
) {
881+
smallestIndex = childIndex;
882+
}
882883
}
883884
if (smallestIndex !== i) {
884885
await this.context.drawAndSwap(arr, i, smallestIndex);
885-
await this.minHeapify(arr, n, smallestIndex);
886+
await this.minHeapify(arr, n, smallestIndex, childCount);
886887
}
887888
}
888889

889-
private async maxHeapify(arr: SortValue[], n: number, i: number) {
890+
private async maxHeapify(
891+
arr: SortValue[],
892+
n: number,
893+
i: number,
894+
childCount: number,
895+
) {
890896
let largestIndex = i;
891-
const left = 2 * i + 1;
892-
const right = 2 * i + 2;
893-
if (
894-
left < n &&
895-
(await this.context.compare(arr, left, '>', largestIndex))
896-
) {
897-
largestIndex = left;
898-
}
899-
if (
900-
right < n &&
901-
(await this.context.compare(arr, right, '>', largestIndex))
902-
) {
903-
largestIndex = right;
897+
for (let c = 0; c < childCount; c++) {
898+
const childIndex = childCount * i + 1 + c;
899+
if (
900+
childIndex < n &&
901+
(await this.context.compare(arr, childIndex, '>', largestIndex))
902+
) {
903+
largestIndex = childIndex;
904+
}
904905
}
906+
905907
if (largestIndex !== i) {
906-
await this.context.drawAndSwap(arr, i, largestIndex);
907-
await this.maxHeapify(arr, n, largestIndex);
908+
await this.context.drawAndSwap(arr, i, largestIndex, childCount);
909+
await this.maxHeapify(arr, n, largestIndex, childCount);
908910
}
909911
}
910912

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type AlgorithmOptions = {
3232
shrinkFactor: number;
3333
heapType: 'max' | 'min';
3434
parallel: boolean;
35+
childCount: number;
3536
};
3637

3738
export enum ResetPreset {

0 commit comments

Comments
 (0)