-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.js
More file actions
36 lines (33 loc) Β· 902 Bytes
/
bubble_sort.js
File metadata and controls
36 lines (33 loc) Β· 902 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
32
33
34
35
36
// Hazrat Ali
// University Of Scholars
const numbers = [23, 54, 12, 8, 45, 90, 2];
// [23, 12, 8, 45, 54, 2, 90];
// [12, 8, 23, 45, 2, 54, 90]
// [8, 12, 23, 2, 45, 54, 90]
function bubbleSort(array){
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
if(array[j] > array[j+1]){
const temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
function bubbleSortDesc(array){
for (let i = 0; i < array.length; i++) {
for (let j = 0; j < array.length; j++) {
if(array[j] < array[j+1]){
const temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
const sorted = bubbleSort(numbers);
console.log(sorted);
// Bubble sort