-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.js
More file actions
37 lines (32 loc) · 878 Bytes
/
selectionSort.js
File metadata and controls
37 lines (32 loc) · 878 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
35
36
37
function selSort(arr) {
for (let i = 0; i < arr.length; i++) {
let lowest= i;
for (let j = i+1; j < arr.length; j++) {
if(arr[j]<arr[lowest]){
lowest = j;
}
}
if(i!==lowest){
let temp = arr[i]
arr[i]=arr[lowest]
arr[lowest]=temp;
console.log(arr, arr[i], arr[lowest])
}
}
return arr;
}
// function selSort(arr) {
// for (let i = 0; i < arr.length; i++) {
// for (let j = i+1; j < arr.length; j++) {
// if(arr[j]<arr[i]){
// let temp = arr[i]
// arr[i]=arr[j]
// arr[j]=temp;
// }
// console.log(arr, arr[i], arr[j])
// }
// }
// return arr;
// }
// More efficient for selSort([5,1,2,3]) took very few steps;
selSort([5,1,2,3,11,4]);