-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.js
More file actions
33 lines (31 loc) · 1.11 KB
/
selectionSort.js
File metadata and controls
33 lines (31 loc) · 1.11 KB
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
// --SELECTION SORT-
function count(iterable){
if(!isNaN(iterable)) iterable = `${iterable}`;
let i = 0;
for(const item of iterable) i++;
return i;
}
// selection sort logic
function selectionSort (list){
let posOfLargest, len = count(list), checks = 0, iterations = 0, checksCount= [];
for(let i = len-1; i>0; i--){
posOfLargest = 0;
for(let j = 0; j<=i; j++){
if(list[j]>list[posOfLargest]) {
posOfLargest = j; checks++;
}
}
checksCount.push(checks);
[list[i], list[posOfLargest]] = [list[posOfLargest], list[i]];
iterations ++;
}
return [list, `No of checks done per iteration: ${checksCount}`, `No of iterations for complete sort ${iterations}`]
}
/**
* Testing the Algo with a normal unordered, ordered, reverse ordered lists
* and a list with only one misplaced element
*/
console.log(selectionSort([1,3,2,4,5,6,7,8,9]));
console.log(selectionSort([13,12,11,9,8,7,6,5,3,2]));
console.log(selectionSort([10,11,22,33,44,55,66,77,88,99]));
console.log(selectionSort([22,3,44,5,6,8,7,10,9,15]));