Skip to content

Commit 12aac74

Browse files
author
Wakidur Rahaman
committed
add function
1 parent dd7edb5 commit 12aac74

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Selection Sort
3+
*/
4+
var selectionSort = function (array) {
5+
for (var i = 0; i < array.length; i++) {
6+
var indexOfMin = i;
7+
for (var j = i + 1; j < array.length; j++) {
8+
if (array[j] < array[indexOfMin]) {
9+
indexOfMin = j;
10+
}
11+
}
12+
if (indexOfMin !== i) {
13+
var lesser = array[indexOfMin];
14+
array[indexOfMin] = array[i];
15+
array[i] = lesser;
16+
}
17+
}
18+
return array;
19+
};
20+
var selection = [3, 4, 9, 3, 1, 2, 4, 5, 6,];
21+
// selectionSort(selection);
22+
console.log(selectionSort(selection));

javascript-today-magazine/selection-sort.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ const selectionSort = (array: props): number[] | string[] => {
2626
const selection: props = [3, 4, 9, 3, 1, 2, 4, 5, 6,]
2727
// selectionSort(selection);
2828

29-
console.log(selection);
29+
console.log(selectionSort(selection));

0 commit comments

Comments
 (0)