-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
19 lines (17 loc) · 783 Bytes
/
app.js
File metadata and controls
19 lines (17 loc) · 783 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//Bubble Sorting. Ascending order
let unsorted = [4, 6, 3, 1, 9];
console.log(unsorted);
// Sorting numbers.
let sorted = [4, 6, 3, 1, 9];
for (let i = 0; i < 4; i++) { // 4 times chalei ga (0,1,2,3)
//for (let j = 0; j <= 3 ; j++) { // aiei bhi likh saktei hai condition mai -i koo remove kar kai bhi. Not Good Practice.
for (let j = 0; j <= 3 - i; j++) { // 4 times chalei ga condition mai equal ka sign hai.
if (sorted[j] > sorted[j + 1]) { // sorted[0] > sorted[1] = 4 > 6
// Swap // sorted[1] > sorted[2] = 6 > 3 condition true
let k = sorted[j]; // k = 6 ;
sorted[j] = sorted[j + 1]; // 6 = 3 ;
sorted[j + 1] = k; // 3 = 6 ;
}
}
}
console.log(sorted); // Output = [1, 3, 4, 6, 9]