-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSelectionSort.js
More file actions
27 lines (24 loc) · 813 Bytes
/
SelectionSort.js
File metadata and controls
27 lines (24 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
* 1. Selection sort is the simplest sorting algorithm. It finds the smallest element in the array and swaps it with the first position in the array. Then the second smallest and swaps it with the second position etc.
* 2. Worst Case Time Complexity : O(n2)
* 3. Best Case Time Complexity : O(n2)
* 4. Average Time Complexity : O(n2)
* 5. Space Complexity : O(1)
* This makes it inefficient to operate on larger datasets
*/
const selectionSort = (array) => {
if(array.length === 1)
return array
for(let i=0;i<array.length;i++){
//set minimum to current index
let min = i
for(let j=i+1;j<array.length;j++){
if(array[j] < array[min])
min = j
}
[array[i],array[min]] = [array[min],array[i]]
}
return array
}
const array = [23,5,87,56,77]
selectionSort(array)