diff --git a/javascript/selectionSort.js b/javascript/selectionSort.js new file mode 100644 index 0000000..9d2b432 --- /dev/null +++ b/javascript/selectionSort.js @@ -0,0 +1,16 @@ +function selectionSort(array) { + for (let i = 0; i < array.length - 1; i++) { + + let minIndex = i; + for (let j = i + 1; j < array.length; j++) { + if (array[j] < array[minIndex]) { + minIndex = j; + } + } + [array[i], array[minIndex]] = [array[minIndex], array[i]]; + } + return array; +} + +const arr = [5, 4, 3, 2, 1]; +console.log(selectionSort(arr));