From 3c56655cedbd3733a0405720b6ef7c52d56417c5 Mon Sep 17 00:00:00 2001 From: Sushant Gangwar Date: Tue, 4 Oct 2022 22:09:48 +0530 Subject: [PATCH] Added Selection Sort using Javascript --- javascript/selectionSort.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 javascript/selectionSort.js 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));