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); +