Skip to content

Commit 61532f5

Browse files
committed
sorting algorithm
1 parent 3778797 commit 61532f5

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

bubble_sort.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//Refer Link:
2+
//https://medium.com/javascript-in-plain-english/simple-sorting-algorithms-in-javascript-57d512ceaf5d
3+
14
const numbers = [10, 20, 23, 2, 8, 70, 22, 33, 6, 55, 97];
25

36
function bubbleSort(array) {
@@ -13,5 +16,20 @@ function bubbleSort(array) {
1316
return array;
1417
}
1518

19+
function bubbleSortDesc(array){
20+
for (let i = 0; i < array.length; i++) {
21+
for (let j = 0; j < array.length; j++) {
22+
if(array[j] < array[j+1]){
23+
const temp = array[j];
24+
array[j] = array[j+1];
25+
array[j+1] = temp;
26+
}
27+
}
28+
}
29+
return array;
30+
}
31+
1632
const sorted = bubbleSort(numbers);
17-
console.log(sorted);
33+
const sortedDes = bubbleSortDesc(numbers);
34+
// console.log(sorted);
35+
// console.log(sortedDes);

0 commit comments

Comments
 (0)