Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 02_selection_sort/javascript/01_selection_sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

// Finds the smallest value in an array
function findSmallest(arr) {
let smallest = arr[0]; // Stores the smallest value
let smallest_index = 0; // Stores the index of the smallest value
for (let i = 1; i < arr.length; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
smallest_index = i;
}
}
return smallest_index;
}

// Sort array
function selectionSort(arr) {
const newArr = [];
for (let i = 0, length = arr.length; i < length; i++) {
// Finds the smallest element in the array and adds it to the new array
let smallest = findSmallest(arr);
newArr.push(arr.splice(smallest, 1)[0]);
}
return newArr;
}

console.log(selectionSort([5, 3, 6, 2, 10])); // [2, 3, 5, 6, 10]