-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselectionSort.js
More file actions
34 lines (32 loc) · 846 Bytes
/
Copy pathselectionSort.js
File metadata and controls
34 lines (32 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
This is an implementation of the selection sort
algorithm.
Idea is to sort a list by successively putting
each value in its final sorted position.
Let n be the size of the list to sort
Time complexity: O(n^2),
Space complexity: O(1)
*/
function selectionSort(arr) {
let min;
// idx is position to fill up with next smallest value
for (let idx = 0; idx < arr.length - 1; idx++) {
min = idx;
// Look for next smallest value in rest of array
for (let scan = idx + 1; scan < arr.length; scan++) {
if (arr[scan] < arr[min]) {
min = scan;
}
}
// Exchange current value with the next smallest value
swap(arr, idx, min);
}
}
function swap(arr, i, j) {
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
const arr = [1, 10, 3, 2];
selectionSort(arr);
console.log(arr); // [ 1, 2, 3, 10 ]