Skip to content

Commit 486ea3b

Browse files
committed
organizing the project and updating few README.md files
1 parent 4f0d59d commit 486ea3b

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
My implementations and solutions of classic and popular algorithms and data structures.
2+
3+
I have put my notes and references for each algorithm and data structure in its own separate README.md file.
4+
5+
### Algorithms and Data Structure by Topic
6+
7+
* **Sorting**
8+
* [Bubble Sort](Sorting/Bubble-Sort)

Sorting/Bubble-Sort/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Bubble Sort
2+
3+
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.
4+
bubble-sort should not be used for larger arrays, can be used for smaller ones for its simplicity.
5+
6+
![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif)
7+
8+
## Complexity
9+
10+
| Name | Best | Average | Worst | Memory | Stable | Comments |
11+
| --------------------- | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- |
12+
| **Bubble sort** | n | n<sup>2</sup> | n<sup>2</sup> | 1 | Yes | |
13+
14+
15+
## MOST IMPORTANT POINT OF BUBBLE SORT (And base of my solution SOLUTION-3 ) :-
16+
17+
A> After the whole first pass is done, (i.e. after the first for loop for i = 0 is run , which also means, for each of i = 0 all the values of the nested for loop for j = 0 to j = arr.length - 1 is run) - The
18+
19+
See this for pictorial representation - http://codingmiles.com/sorting-algorithms-bubble-sort-using-javascript/
20+
21+
B> In other words, after the first pass, the biggest elements is placed at its correct position.
22+
23+
C) And this IS THE PRINCIPLE BASED ON WHICH I CAN IMPROVE THE ALGO BY REDUCING THE NO OF PASSES FOR THE INNER NESTED FOR LOOP EACH TIME - Meaning, for the second loop of i = 1 pass, I actually no more have to compare the right-most element. As I know the right-most element is the biggest and has been placed at its correct right-most position.
24+
25+
D) Then for for the next loop i=2, I can reduce the no of comarison for the nested for loop by 2, because I know, that the biggest 2 element has already been placed to the farthest right side.

Sorting/bubble-sort-basic-FUTURE-REFERENCE.js renamed to Sorting/Bubble-Sort/bubble-sort-basic-FUTURE-REFERENCE.js

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
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-
bubble-sort should not be used for larger arrays, can be used for smaller ones for its simplicity.
3-
4-
5-
MOST IMPORTANT POINT OF BUBBLE SORT (And base of my solution SOLUTION-3 ) :-
6-
7-
A> After the whole first pass is done, (i.e. after the first for loop for i = 0 is run , which also means, for each of i = 0 all the values of the nested for loop for j = 0 to j = arr.length - 1 is run) - The
8-
9-
See this for pictorial representation - http://codingmiles.com/sorting-algorithms-bubble-sort-using-javascript/
10-
11-
B> In other words, after the first pass, the biggest elements is placed at its correct position.
12-
13-
C) And this IS THE PRINCIPLE BASED ON WHICH I CAN IMPROVE THE ALGO BY REDUCING THE NO OF PASSES FOR THE INNER NESTED FOR LOOP EACH TIME - Meaning, for the second loop of i = 1 pass, I actually no more have to compare the right-most element. As I know the right-most element is the biggest and has been placed at its correct right-most position.
14-
15-
D) Then for for the next loop i=2, I can reduce the no of comarison for the nested for loop by 2, because I know, that the biggest 2 element has already been placed to the farthest right side.
16-
17-
*/
18-
191
let arr = Array.from({ length: 20}, () => Math.floor(Math.random() * 20))
202
// console.log(arr);
213
var array1 = [9, 2, 5, 6, 4, 3, 7, 10, 1, 8];
@@ -50,7 +32,7 @@ bubbleSortBasicAscending1 = (arr) => {
5032
return arr;
5133
}
5234

53-
console.log(bubbleSortBasicAscending1(array1));
35+
// console.log(bubbleSortBasicAscending1(array1));
5436

5537
// 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]
5638
// Learning JavaScript Data Structures and Algorithms-Loiane.pdf - Page - 225
@@ -81,7 +63,7 @@ bubbleSortBasicDescending = (arr) => {
8163
return arr;
8264
}
8365

84-
console.log(bubbleSortBasicDescending(array1));
66+
// console.log(bubbleSortBasicDescending(array1));
8567

8668
/* SOLUTION - 2 - A more optimal solution, by reducing some of the loop execution.
8769
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
@@ -91,7 +73,8 @@ And each time there's something to swap the do-while loop will continue, and the
9173
*/
9274
bubbleSortAscending_Improved = arr => {
9375

94-
let swapped;
76+
// A flag that holds whether the swapping happened or not
77+
let swapped = false;
9578

9679
do {
9780
swapped = false;

0 commit comments

Comments
 (0)