Skip to content

Commit 73c0643

Browse files
author
Oskar Widmark
committed
feat: add fold sort and crease sort
1 parent c684de5 commit 73c0643

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

src/ColumnSlider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export function ColumnSlider(props: {
1919
const requiresPowerOfTwoColumns =
2020
chosenSortAlg === SortName.BitonicSort ||
2121
(chosenSortAlg === SortName.OddEvenMergesort &&
22-
algorithmOptions.type === 'recursive');
22+
algorithmOptions.type === 'recursive') ||
23+
chosenSortAlg === SortName.FoldSort;
2324

2425
useEffect(() => {
2526
if (requiresPowerOfTwoColumns) {

src/Options.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const getAlgorithmOptionFields = (
1717
case SortName.OddEvenMergesort:
1818
return ['type', 'parallel'];
1919
case SortName.OddEvenSort:
20+
case SortName.FoldSort:
21+
case SortName.CreaseSort:
2022
return ['parallel'];
2123
case SortName.RadixSortLSD:
2224
case SortName.RadixSortMSD:

src/sorting-algorithms.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export class SortingAlgorithms {
2525
[SortName.AverageSort]: this.averageSort,
2626
[SortName.Heapsort]: this.heapsort,
2727
[SortName.PushSort]: this.pushSort,
28+
[SortName.FoldSort]: this.foldSort,
29+
[SortName.CreaseSort]: this.creaseSort,
2830
// 'Bully Sort 2': this.bullySort2,
2931
};
3032

@@ -933,4 +935,96 @@ public async mergeSort(arr, start, end){
933935
}
934936
}
935937
}
938+
939+
public async foldSort(arr: SortValue[], options: AlgorithmOptions) {
940+
let drawIteration = 0;
941+
for (
942+
let m = 2 ** (Math.floor(Math.log2(arr.length)) - 1);
943+
m > 0;
944+
m = Math.floor(m / 2)
945+
) {
946+
for (
947+
let k = 2 ** (Math.floor(Math.log2(arr.length)) - 1);
948+
k >= Math.floor((m + 1) / 2);
949+
k = Math.floor(k / 2)
950+
) {
951+
const fns = [];
952+
953+
for (let j = k; j < arr.length; j += 2 * k) {
954+
for (let i = 0; i < k && j + i < arr.length; i++) {
955+
fns.push(async () => {
956+
if (
957+
await this.context.compare(
958+
arr,
959+
j - 1 - i,
960+
'>',
961+
j + i,
962+
options.parallel ? drawIteration : undefined,
963+
)
964+
) {
965+
await this.context.drawAndSwap(arr, j - 1 - i, j + i);
966+
}
967+
});
968+
}
969+
}
970+
await runFunctions(fns, options.parallel);
971+
drawIteration++;
972+
}
973+
}
974+
}
975+
976+
public async creaseSort(arr: SortValue[], options: AlgorithmOptions) {
977+
let drawIteration = 0;
978+
for (
979+
let k = 2 ** Math.floor(Math.log2(arr.length - 1));
980+
k > 0;
981+
k = Math.floor(k / 2)
982+
) {
983+
const fns = [];
984+
for (let i = 1; i < arr.length; i += 2) {
985+
fns.push(async () => {
986+
if (
987+
await this.context.compare(
988+
arr,
989+
i - 1,
990+
'>',
991+
i,
992+
options.parallel ? drawIteration : undefined,
993+
)
994+
) {
995+
await this.context.drawAndSwap(arr, i - 1, i);
996+
}
997+
});
998+
}
999+
1000+
await runFunctions(fns, options.parallel);
1001+
drawIteration++;
1002+
1003+
for (
1004+
let j = 2 ** Math.floor(Math.log2(arr.length - 1));
1005+
j >= k && j > 1;
1006+
j = Math.floor(j / 2)
1007+
) {
1008+
const fns = [];
1009+
for (let i = j; i < arr.length; i += 2) {
1010+
fns.push(async () => {
1011+
if (
1012+
await this.context.compare(
1013+
arr,
1014+
i - j + 1,
1015+
'>',
1016+
i,
1017+
options.parallel ? drawIteration : undefined,
1018+
)
1019+
) {
1020+
await this.context.drawAndSwap(arr, i - j + 1, i);
1021+
}
1022+
});
1023+
}
1024+
1025+
await runFunctions(fns, options.parallel);
1026+
drawIteration++;
1027+
}
1028+
}
1029+
}
9361030
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export enum SortName {
1717
AverageSort = 'Average Sort',
1818
Heapsort = 'Heapsort',
1919
PushSort = 'Push Sort',
20+
FoldSort = 'Fold Sort',
21+
CreaseSort = 'Crease Sort',
2022
}
2123

2224
export type SortAlgorithm = (

0 commit comments

Comments
 (0)