Skip to content

Commit 18ef8cb

Browse files
committed
alt solution of bubble sort un-optimized
1 parent a5b9363 commit 18ef8cb

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Sorting/bubble-sort-Loiane-Book/bubble-sort--optimized.js

Whitespace-only changes.

Sorting/bubble-sort-basic-FUTURE-REFERENCE.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* When people first start learning sorting algorithms, they usually learn the bubble sort algorithm first, because it is the simplest of all the sorting algorithms. However, it is one of the worst-case sorting algorithms with respect to runtime. The bubble sort algorithm compares every two adjacent items and swaps them if the first one is bigger than the second one. It has this name because the items tend to move up into the correct order, like bubbles rising to the surface.
2+
*/
3+
14
let arr = Array.from({ length: 20}, () => Math.floor(Math.random() * 20))
25
// console.log(arr);
36
var array1 = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
@@ -20,7 +23,7 @@ swap = (arr, i, j) => [arr[i], arr[j]] = [arr[j], arr[i]]
2023

2124
// SOLUTION-1 - Necessary warning :) :) - this is a super basic implementation to understand the principle behind bubble sort (going through all comparisons) but there's huge room for performance improvement on this.
2225

23-
bubbleSortBasicAscending = (arr) => {
26+
bubbleSortBasicAscending1 = (arr) => {
2427

2528
for (let i = 0; i < arr.length; i++) {
2629
for (let j = 1; j < arr.length; j++) {
@@ -32,7 +35,23 @@ bubbleSortBasicAscending = (arr) => {
3235
return arr;
3336
}
3437

35-
console.log(bubbleSortBasicAscending(array1));
38+
console.log(bubbleSortBasicAscending1(array1));
39+
40+
// Now the same solution but starting the nested loop from 0-th index instead of 1 (as above) and going upto (arr.length - 1) instead of arr.length. And so the previous solution, I was comparing < if (arr[j] < arr[j - 1]) > and here I shall compare arr[j + 1] < arr[j] . And thats why I am running the loop upto (arr.length - 1) because inside I will take care of the element arr[j+1]
41+
42+
bubbleSortBasicAscending2 = arr => {
43+
44+
for (let i = 0; i < arr.length; i++ ) {
45+
for (let j = 0; j < arr.length - 1; j++) {
46+
if (arr[j] > arr[j + 1]) {
47+
swap(arr, j, j + 1)
48+
}
49+
}
50+
}
51+
return arr;
52+
}
53+
54+
console.log(bubbleSortBasicAscending2(array1));
3655

3756
bubbleSortBasicDescending = (arr) => {
3857

@@ -48,10 +67,11 @@ bubbleSortBasicDescending = (arr) => {
4867

4968
console.log(bubbleSortBasicDescending(array1));
5069

51-
/* SOLUTION - 2 - A more optimal solution, by reducing some of the loop execution are not done in this solution.
70+
/* SOLUTION - 2 - A more optimal solution, by reducing some of the loop execution.
5271
So, here, I only do the loops and swaps for the cases when I find a mis-placed element, i.e. larger-element placed before smaller in an ascending sort
72+
So, here, I traverse the array only once. And only the first time, I find a mis-placed element, that is I do an actual swap, do I make that variable 'swap' to be true.
5373
*/
54-
bubbleSortAscending = arr => {
74+
bubbleSortAscending_Improved = arr => {
5575

5676
let swapped;
5777

@@ -68,4 +88,4 @@ bubbleSortAscending = arr => {
6888
}
6989

7090

71-
console.log(bubbleSortAscending(array1));
91+
console.log(bubbleSortAscending_Improved(array1));

0 commit comments

Comments
 (0)