Skip to content

Commit

Permalink
New algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
mszula committed Jun 26, 2024
1 parent fde9696 commit 612fd8b
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 81 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ https://github.com/mszula/visual-sorting/assets/17621834/3077ad21-ed6e-432c-ae51
## 🤖 Supported Sorting Algorithms

- Bubble Sort
- Selection Sort
- Insertion Sort
- Quick Sort
- Shell Sort
- Merge Sort
- Radix Sort
- Insertion Sort
- Selection Sort
- Radix LSD Sort
- Radix MSD Sort
- Heap Sort
- Bitonic Sort
- Tim Sort
Expand All @@ -35,6 +36,8 @@ https://github.com/mszula/visual-sorting/assets/17621834/3077ad21-ed6e-432c-ae51
- Pancake Sort
- Stooge Sort
- Bogo Sort
- Exchange Sort
- Odd Even Sort

## 🙌 Contribution

Expand Down
42 changes: 30 additions & 12 deletions src/lib/components/AlgorithmSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,46 @@
export let selectAlgorithm: (algo: AlgorithmDefinition) => void;
import { algorithms } from '../sort-algorithms/algorithms';
import type { AlgorithmDefinition } from '../sort-algorithms/types';
import ArrowRight from './algorithm-selector/ArrowRight.svelte';
import ArrowLeft from './algorithm-selector/ArrowLeft.svelte';
let selected = 0;
let selectedGroup = 0;
const click = (index: number) => () => {
const click = (group: number, index: number) => () => {
selected = index;
selectAlgorithm(algorithms[index]);
selectedGroup = group;
selectAlgorithm(algorithms[group][index]);
};
onMount(() => {
selectAlgorithm(algorithms[selected]);
selectAlgorithm(algorithms[selectedGroup][selected]);
});
</script>

<div class="flex bg-base-200 rounded-box mb-2 md:mb-0 md:mr-5">
<ul class="menu grid grid-rows-4 grid-flow-col">
{#each algorithms as algo, index}
<li>
<button
class={selected === index ? 'active' : ''}
on:click={click(index)}>{algo.name}</button
>
</li>
<div class="carousel rounded-box w-[30.5rem]">
{#each algorithms as algos, group}
<div id="group{group}" class="carousel-item w-full">
<ul class="menu grid grid-rows-4 grid-flow-col">
{#each algos as algo, index}
<li>
<button
class={selected === index && selectedGroup === group
? 'active'
: ''}
on:click={click(group, index)}>{algo.name}</button
>
</li>
{#if index === 2 && group > 0}
<ArrowLeft {group} />
{/if}
{/each}
{#if group !== algorithms.length - 1}
<ArrowRight {group} />
{/if}
</ul>
</div>
{/each}
</ul>
</div>
</div>
25 changes: 25 additions & 0 deletions src/lib/components/algorithm-selector/ArrowLeft.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script lang="ts">
export let group = 0;
</script>

<li>
<a href="#group{group - 1}" class="flex justify-center"
><svg
class="w-[20px] h-[20px] text-primary"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
fill="none"
viewBox="0 0 24 24"
>
<path
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M5 12h14M5 12l4-4m-4 4 4 4"
/>
</svg>
</a>
</li>
25 changes: 25 additions & 0 deletions src/lib/components/algorithm-selector/ArrowRight.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script lang="ts">
export let group = 0;
</script>

<li>
<a href="#group{group + 1}" class="flex justify-center"
><svg
class="w-[20px] h-[20px] text-primary"
aria-hidden="true"
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
fill="none"
viewBox="0 0 24 24"
>
<path
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M19 12H5m14 0-4 4m4-4-4-4"
/>
</svg>
</a>
</li>
151 changes: 85 additions & 66 deletions src/lib/sort-algorithms/algorithms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,102 @@ import { bogoSort } from './bogo-sort';
import { bubleSort } from './buble-sort';
import { cocktailSort } from './cocktail-sort';
import { cycleSort } from './cycle-sort';
import { exchangeSort } from './exchange-sort';
import { gnomeSort } from './gnome-sort';
import { heapSort } from './heap-sort';
import { insertionSort } from './insertion-sort';
import { mergeSort } from './merge-sort';
import { oddEvenSort } from './odd-even-sort';
import { pancakeSort } from './pancake-sort';
import { quickSort } from './quick-sort';
import { radixSort } from './radix-sort';
import { radixSortMSD } from './radix-sort-msd';
import { selectionSort } from './selection-sort';
import { shellSort } from './shell-sort';
import { stoogeSort } from './stooge-sort';
import { timSort } from './tim-sort';
import type { AlgorithmDefinition } from './types';

export const algorithms: AlgorithmDefinition[] = [
{
name: 'Bubble Sort',
function: bubleSort,
},
{
name: 'Insertion Sort',
function: insertionSort,
},
{
name: 'Selection Sort',
function: selectionSort,
},
{
name: 'Quick Sort',
function: quickSort,
},
{
name: 'Shell Sort',
function: shellSort,
},
{
name: 'Merge Sort',
function: mergeSort,
},
{
name: 'Radix Sort',
function: radixSort,
},
{
name: 'Heap Sort',
function: heapSort,
},
{
name: 'Bitonic Sort',
function: bitonicSort,
arraySizeComponent: RangeArraySizePowerOfTwo,
},
{
name: 'Tim Sort',
function: timSort,
},
{
name: 'Gnome Sort',
function: gnomeSort,
},
{
name: 'Cycle Sort',
function: cycleSort,
},
{
name: 'Cocktail Sort',
function: cocktailSort,
},
{
name: 'Pancake Sort',
function: pancakeSort,
},
{
name: 'Stooge Sort',
function: stoogeSort,
},
{
name: 'Bogo Sort',
function: bogoSort,
},
export const algorithms: AlgorithmDefinition[][] = [
[
{
name: 'Bubble Sort',
function: bubleSort,
},
{
name: 'Quick Sort',
function: quickSort,
},
{
name: 'Shell Sort',
function: shellSort,
},
{
name: 'Merge Sort',
function: mergeSort,
},
{
name: 'Insertion Sort',
function: insertionSort,
},
{
name: 'Selection Sort',
function: selectionSort,
},
{
name: 'Radix LSD Sort',
function: radixSort,
},
{
name: 'Radix MSD Sort',
function: radixSortMSD,
},
{
name: 'Heap Sort',
function: heapSort,
},
{
name: 'Bitonic Sort',
function: bitonicSort,
arraySizeComponent: RangeArraySizePowerOfTwo,
},
{
name: 'Tim Sort',
function: timSort,
},
{
name: 'Gnome Sort',
function: gnomeSort,
},
{
name: 'Cycle Sort',
function: cycleSort,
},
{
name: 'Cocktail Sort',
function: cocktailSort,
},
{
name: 'Pancake Sort',
function: pancakeSort,
},
],
[
{
name: 'Stooge Sort',
function: stoogeSort,
},
{
name: 'Bogo Sort',
function: bogoSort,
},
{
name: 'Exchange Sort',
function: exchangeSort,
},
{
name: 'Odd Even Sort',
function: oddEvenSort,
},
],
];
17 changes: 17 additions & 0 deletions src/lib/sort-algorithms/exchange-sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { SortingGenerator } from './types';

export const exchangeSort = function* (arr: number[]): SortingGenerator {
let n = arr.length;

for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
yield { access: [i, j], sound: j };

if (arr[i] > arr[j]) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
};
38 changes: 38 additions & 0 deletions src/lib/sort-algorithms/odd-even-sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Javascript Program to implement
// Odd-Even / Brick Sort

import type { SortingGenerator } from './types';

export const oddEvenSort = function* (arr: number[]): SortingGenerator {
// Initially array is unsorted
const n = arr.length;

let isSorted = false;

while (!isSorted) {
isSorted = true;
let temp = 0;

// Perform Bubble sort on odd indexed element
for (let i = 1; i <= n - 2; i = i + 2) {
yield { access: [i, i + 1], sound: i };
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}

// Perform Bubble sort on even indexed element
for (let i = 0; i <= n - 2; i = i + 2) {
yield { access: [i, i + 1], sound: i };
if (arr[i] > arr[i + 1]) {
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
isSorted = false;
}
}
}
};
Loading

0 comments on commit 612fd8b

Please sign in to comment.