Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Sort/SelectionSort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { SortBase, type SortCoreElement } from "./SortBase";

export class SelectionSort extends SortBase {
public core<T>(content: SortCoreElement<T>): SortCoreElement<T> {
const baseArr = content.concat();
const arr: SortCoreElement<T> = [];
do {
let minIndex = 0;
let min = baseArr[minIndex] as SortCoreElement<T>[number];
for (let i = 0; i < baseArr.length; i++) {
const element = baseArr[i] as SortCoreElement<T>[number];
if (element.num < min.num) {
minIndex = i;
min = element;
}
}
baseArr.splice(minIndex, 1);
arr.push(min);
} while (arr.length !== content.length);
return arr;
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BubbleSort } from "./Sort/BubbleSort";
import { BogoSort } from "./Sort/BogoSort";
import { QuickSort } from "./Sort/QuickSort";
import { InsertionSort } from "./Sort/InsertionSort";
import { SelectionSort } from "./Sort/SelectionSort";

import type { SortElement } from "./Sort/SortBase";

Expand All @@ -10,6 +11,7 @@ export class SortMan {
public static readonly bubble = new BubbleSort();
public static readonly quick = new QuickSort();
public static readonly insertion = new InsertionSort();
public static readonly selection = new SelectionSort();

public static sort<T>(content: SortElement<T>): T[] {
return this.bubble.sort(content);
Expand Down
6 changes: 5 additions & 1 deletion tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ export const sorts: Sorts = [
algorithm: SortMan.quick
},
{
name: "Insertion Sort",
name: "Insertion sort",
algorithm: SortMan.insertion
},
{
name: "Selection sort",
algorithm: SortMan.selection
}
];