-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.js
More file actions
21 lines (19 loc) · 833 Bytes
/
selectionSort.js
File metadata and controls
21 lines (19 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function selectionSort(arrayToBeSorted) {
for (let pointer1 = 0; pointer1 < arrayToBeSorted.length - 1; pointer1++) {
let smallestValueIndex = pointer1;
for (
let pointer2 = pointer1 + 1;
pointer2 < arrayToBeSorted.length;
pointer2++
) {
if (arrayToBeSorted[smallestValueIndex] > arrayToBeSorted[pointer2]) {
smallestValueIndex = pointer2;
}
}
let tempPointer = arrayToBeSorted[pointer1]; //saves the current pointer1 value
arrayToBeSorted[pointer1] = arrayToBeSorted[smallestValueIndex]; //sets the smallest value to the current pointer1
arrayToBeSorted[smallestValueIndex] = tempPointer; //sets the large value to the position of the smallest value
}
return arrayToBeSorted;
}
console.log(selectionSort([100, 1, 3, 8, 1, 2, 90, 4, 10, 6, 30, 5, 0, 7, 0]));