Skip to content

Commit 18bafe3

Browse files
Merge pull request #589 from NisargChokshi45/feat/selectionsort
Adds Selection sort implementation in JavaScript and TypeScript
2 parents 127dab9 + 42c9bed commit 18bafe3

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* Selection Sort
2+
3+
Description: Selection sort is a simple comparison-based sorting algorithm. It works by repeatedly finding the minimum element from the unsorted portion of the array and placing it at the beginning. The algorithm divides the array into two parts: sorted and unsorted. In each iteration, it selects the smallest element from the unsorted part and swaps it with the first element of the unsorted part.
4+
5+
Time Complexity: O(n²)
6+
7+
Space Complexity: O(1)
8+
*/
9+
10+
function selectionSort(arr) {
11+
const n = arr.length;
12+
13+
for (let i = 0; i < n - 1; i++) {
14+
let minIndex = i;
15+
16+
for (let j = i + 1; j < n; j++) {
17+
if (arr[j] < arr[minIndex]) {
18+
minIndex = j;
19+
}
20+
}
21+
22+
// Swap the found minimum element with the first element
23+
if (minIndex !== i) {
24+
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
25+
}
26+
}
27+
28+
return arr;
29+
}
30+
31+
function runTests() {
32+
console.log("---------- Selection Sort Test Cases ----------");
33+
34+
// Test 1: Regular unsorted array
35+
const test1 = [64, 34, 25, 12, 22, 11, 90];
36+
console.log("\nTest 1 - Original:", [...test1]);
37+
console.log("Test 1 - Sorted:", selectionSort([...test1]));
38+
39+
// Test 2: Already sorted array
40+
const test2 = [1, 2, 3, 4, 5];
41+
console.log("\nTest 2 - Original:", [...test2]);
42+
console.log("Test 2 - Sorted:", selectionSort([...test2]));
43+
44+
// Test 3: Reverse sorted array
45+
const test3 = [5, 4, 3, 2, 1];
46+
console.log("\nTest 3 - Original:", [...test3]);
47+
console.log("Test 3 - Sorted:", selectionSort([...test3]));
48+
49+
// Test 4: Array with duplicates
50+
const test4 = [3, 1, 4, 1, 5, 9, 2, 6, 5];
51+
console.log("\nTest 4 - Original:", [...test4]);
52+
console.log("Test 4 - Sorted:", selectionSort([...test4]));
53+
54+
// Test 5: Single element array
55+
const test5 = [42];
56+
console.log("\nTest 5 - Original:", [...test5]);
57+
console.log("Test 5 - Sorted:", selectionSort([...test5]));
58+
59+
// Test 6: Empty array
60+
const test6 = [];
61+
console.log("\nTest 6 - Original:", [...test6]);
62+
console.log("Test 6 - Sorted:", selectionSort([...test6]));
63+
64+
// Test 7: Two element array
65+
const test7 = [2, 1];
66+
console.log("\nTest 7 - Original:", [...test7]);
67+
console.log("Test 7 - Sorted:", selectionSort([...test7]));
68+
69+
// Test 8: Array with negative numbers
70+
const test8 = [-5, 2, -3, 8, -1, 0];
71+
console.log("\nTest 8 - Original:", [...test8]);
72+
console.log("Test 8 - Sorted:", selectionSort([...test8]));
73+
}
74+
75+
runTests();
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* Selection Sort
2+
3+
Description: Selection sort is a simple comparison-based sorting algorithm. It works by repeatedly finding the minimum element from the unsorted portion of the array and placing it at the beginning. The algorithm divides the array into two parts: sorted and unsorted. In each iteration, it selects the smallest element from the unsorted part and swaps it with the first element of the unsorted part.
4+
5+
Time Complexity: O(n²)
6+
7+
Space Complexity: O(1)
8+
*/
9+
10+
function selectionSort(arr: number[]): number[] {
11+
const n: number = arr.length;
12+
13+
for (let i = 0; i < n - 1; i++) {
14+
let minIndex: number = i;
15+
16+
for (let j = i + 1; j < n; j++) {
17+
if (arr[j] < arr[minIndex]) {
18+
minIndex = j;
19+
}
20+
}
21+
22+
// Swap the found minimum element with the first element
23+
if (minIndex !== i) {
24+
[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
25+
}
26+
}
27+
28+
return arr;
29+
}
30+
31+
function runTests(): void {
32+
console.log("---------- Selection Sort Test Cases ----------");
33+
34+
// Test 1: Regular unsorted array
35+
const test1: number[] = [64, 34, 25, 12, 22, 11, 90];
36+
console.log("\nTest 1 - Original:", [...test1]);
37+
console.log("Test 1 - Sorted:", selectionSort([...test1]));
38+
39+
// Test 2: Already sorted array
40+
const test2: number[] = [1, 2, 3, 4, 5];
41+
console.log("\nTest 2 - Original:", [...test2]);
42+
console.log("Test 2 - Sorted:", selectionSort([...test2]));
43+
44+
// Test 3: Reverse sorted array
45+
const test3: number[] = [5, 4, 3, 2, 1];
46+
console.log("\nTest 3 - Original:", [...test3]);
47+
console.log("Test 3 - Sorted:", selectionSort([...test3]));
48+
49+
// Test 4: Array with duplicates
50+
const test4: number[] = [3, 1, 4, 1, 5, 9, 2, 6, 5];
51+
console.log("\nTest 4 - Original:", [...test4]);
52+
console.log("Test 4 - Sorted:", selectionSort([...test4]));
53+
54+
// Test 5: Single element array
55+
const test5: number[] = [42];
56+
console.log("\nTest 5 - Original:", [...test5]);
57+
console.log("Test 5 - Sorted:", selectionSort([...test5]));
58+
59+
// Test 6: Empty array
60+
const test6: number[] = [];
61+
console.log("\nTest 6 - Original:", [...test6]);
62+
console.log("Test 6 - Sorted:", selectionSort([...test6]));
63+
64+
// Test 7: Two element array
65+
const test7: number[] = [2, 1];
66+
console.log("\nTest 7 - Original:", [...test7]);
67+
console.log("Test 7 - Sorted:", selectionSort([...test7]));
68+
69+
// Test 8: Array with negative numbers
70+
const test8: number[] = [-5, 2, -3, 8, -1, 0];
71+
console.log("\nTest 8 - Original:", [...test8]);
72+
console.log("Test 8 - Sorted:", selectionSort([...test8]));
73+
}
74+
75+
runTests();

0 commit comments

Comments
 (0)