From e9a9867b3dfc3aaf0c6e0336ea1a9eddc3a81fcf Mon Sep 17 00:00:00 2001 From: Sarah Sakhri Date: Fri, 4 Oct 2019 21:17:23 -0400 Subject: [PATCH] selectionsort/sarahsakhri --- SortingAlgorithms/selectionSort.js | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 SortingAlgorithms/selectionSort.js diff --git a/SortingAlgorithms/selectionSort.js b/SortingAlgorithms/selectionSort.js new file mode 100644 index 0000000..f928f40 --- /dev/null +++ b/SortingAlgorithms/selectionSort.js @@ -0,0 +1,33 @@ +//Language: Javascript +//Author: Sarah Sakhri +//Github: https://github.com/sarahsakhri + +let x = [66, 0, 19, -45, 26, 56, 15, 21, -98]; +console.log("---- Original array -----"); +console.log(x); + + +function selectionSort(arr) { + for (i = 0; i < arr.length; i++) { + let min = i; + for (let j = i + 1; j < arr.length; j++) { + if (arr[j] < arr[min]) { + min = j; + } + } + + if (i !== min) { + let temp = arr[i]; + arr[i] = arr[min]; + arr[min] = temp; + } + } + + return arr; +} + +selectionSort(x); + +console.log("---- Selection sorted array -----"); +console.log(x); +