Skip to content

Commit c5b3fae

Browse files
committed
add: selection sort algorithm implementation
1 parent 2b6af2d commit c5b3fae

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

Sorting/SelectionSort.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { swap } from "../Arrays/Swap";
2+
3+
/**
4+
* Sorts an array in ascending order using the Selection Sort algorithm.
5+
*
6+
* @param {number[]} arr - The input array to be sorted.
7+
* @returns {number[]} - The sorted array.
8+
*
9+
* @description
10+
* Selection Sort is a simple sorting algorithm that repeatedly selects the minimum element from the unsorted part of the array
11+
* and places it at the beginning. The algorithm maintains two subarrays: one that is sorted and one that is unsorted.
12+
*
13+
* @complexity
14+
* Time complexity:
15+
* - Best-case time complexity: O(n^2) - In all cases, Selection Sort performs the same number of comparisons and swaps, resulting in a quadratic time complexity.
16+
* - Average-case time complexity: O(n^2) - The algorithm's performance is similar for all cases, leading to a quadratic time complexity.
17+
* - Worst-case time complexity: O(n^2) - Selection Sort always performs n*(n-1)/2 comparisons and n-1 swaps, resulting in a quadratic time complexity.
18+
* Space complexity: O(1) - Constant space complexity, as the sorting is done in-place.
19+
*/
20+
function selectionSort(arr) {
21+
const n = arr.length;
22+
23+
for (let i = 0; i < n - 1; i++) {
24+
let minIndex = i;
25+
26+
// Find the index of the minimum element in the unsorted part of the array
27+
for (let j = i + 1; j < n; j++) {
28+
if (arr[j] < arr[minIndex]) {
29+
minIndex = j;
30+
}
31+
}
32+
33+
// Swap the minimum element with the first element in the unsorted part
34+
if (minIndex !== i) {
35+
swap(arr, i, minIndex);
36+
}
37+
}
38+
39+
return arr;
40+
}
41+
42+
export { selectionSort };
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { selectionSort } from "../SelectionSort";
2+
3+
describe("selectionSort", () => {
4+
it("should sort an array of integers in ascending order (best case)", () => {
5+
const sortedArray = [1, 2, 3, 4, 5];
6+
const result = selectionSort(sortedArray);
7+
8+
expect(result).toEqual(sortedArray);
9+
});
10+
11+
it("should sort an array of integers in ascending order (average case)", () => {
12+
const unsortedArray = [64, 25, 12, 22, 11];
13+
const expectedArray = [11, 12, 22, 25, 64];
14+
const sortedArray = selectionSort(unsortedArray);
15+
16+
expect(sortedArray).toEqual(expectedArray);
17+
});
18+
19+
it("should sort an array of integers in ascending order (worst case)", () => {
20+
const reverseSortedArray = [5, 4, 3, 2, 1];
21+
const expectedArray = [1, 2, 3, 4, 5];
22+
const sortedArray = selectionSort(reverseSortedArray);
23+
24+
expect(sortedArray).toEqual(expectedArray);
25+
});
26+
27+
it("should handle an empty array", () => {
28+
const emptyArray = [];
29+
const sortedArray = selectionSort(emptyArray);
30+
31+
expect(sortedArray).toEqual([]);
32+
});
33+
34+
it("should handle an array with a single element", () => {
35+
const singleElementArray = [42];
36+
const sortedArray = selectionSort(singleElementArray);
37+
38+
expect(sortedArray).toEqual([42]);
39+
});
40+
});

0 commit comments

Comments
 (0)