From 30e148143a9484fe4b3cd8b7bcd69efa45a95b8b Mon Sep 17 00:00:00 2001 From: rohit Date: Sun, 1 Oct 2023 21:33:21 +0530 Subject: [PATCH] added selection sort in javaScript --- algorithms/javascript/selection_sort.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 algorithms/javascript/selection_sort.js diff --git a/algorithms/javascript/selection_sort.js b/algorithms/javascript/selection_sort.js new file mode 100644 index 0000000..358862f --- /dev/null +++ b/algorithms/javascript/selection_sort.js @@ -0,0 +1,15 @@ +function selectionSort(array) { + for (let i = 0; i < array.length; 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; + } \ No newline at end of file