-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.js
More file actions
35 lines (26 loc) · 846 Bytes
/
selectionSort.js
File metadata and controls
35 lines (26 loc) · 846 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
28
29
30
31
// Selection Sort
// The selection sort algorithm sorts an array by repeatedly finding the minimum
// element (considering ascending order) from unsorted part and putting it at the beginning.
//The algorithm maintains two subarrays in a given array.
// 1) The subarray which is already sorted.
// 2) Remaining subarray which is unsorted.
// In every iteration of selection sort, the minimum element (considering ascending order)
//from the unsorted subarray is picked and moved to the sorted subarray.
function selectionSort (array){
var result = [];
function sort (arr){
var min = array[0];
for(var i = 0; i < arr.length; i++){
if(arr[i] < min){
min = arr[i];
}
}
result.push(min);
arr.splice(arr.indexOf(min), 1)
if(!arr.length){
return result;
}
return sort(arr);
}
return selectionSort(array);
}