-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort20230708.js
38 lines (30 loc) · 1.44 KB
/
SelectionSort20230708.js
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
35
36
37
38
// Selection sort
// Go through the array and keep track of the lowest number's index
// If we find a number lower than where we started, we swap that number with the number we started our pass through with
// Selection Sort Best: O((n^2 /2)) but O(n^2) because Big O Notation ignores constants
// Selection Sort Average: O((n^2 /2)) but O(n^2) because Big O Notation ignores constants
// Selection Sort Worst: O((n^2 /2)) but O(n^2) because Big O Notation ignores constants
// Selection Sort Space: O(1)
// Still twice as fast than bubble sort because we only make 1 swap per pass instead of multiple swaps
const selectionSort = (array = []) => {
// x = starting index number for our pass
// y = current index for iterration within the pass
for(let x = 0; x < array.length - 1; x++) {
let lowestNumberIndex = x;
// We always start the pass 1 index ahead
// This is a O(n squared) operation
for(let y = x + 1; y < array.length; y++) {
if(array[y] < array[lowestNumberIndex]) {
// New lowest number array
lowestNumberIndex = y;
}
}
if(lowestNumberIndex != x) {
let oldLowestNumber = array[x];
array[x] = array[lowestNumberIndex];
array[lowestNumberIndex] = oldLowestNumber;
}
}
return array;
};
console.log(selectionSort([4,2,7,1,3]));