Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit63a11fe

Browse files
committed
refactoring bubble sort
1 parentada2b3e commit63a11fe

File tree

3 files changed

+64
-20
lines changed

3 files changed

+64
-20
lines changed

‎Sort/BubbleSort/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ A> After the whole first pass is done, (i.e. after the first for loop for i = 0
1818

1919
See this for pictorial representation -http://codingmiles.com/sorting-algorithms-bubble-sort-using-javascript/
2020

21+
So basically, the bubble sort algorithm can be easily optimized by observing that the n-th pass finds the n-th largest element and puts it into its final place. So, the inner loop can avoid looking at the last n − 1 items when running for the n-th time:
22+
2123
B> In other words, after the first pass, the biggest elements is placed at its correct position.
2224

2325
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.

‎Sort/BubbleSort/bubble-sort-Loiane-Book/bubble-sort--optimized.js

Whitespace-only changes.

‎Sort/BubbleSort/bubble-sort-basic-FUTURE-REFERENCE.js

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var x = 'a';
1616
var y = 'b';
1717
Book- Learning JavaScript Data Structures and Algorithms.pdf // page-40 */
1818

19-
swap=(arr,i,j)=>[arr[i],arr[j]]=[arr[j],arr[i]]
19+
constswap=(arr,i,j)=>[arr[i],arr[j]]=[arr[j],arr[i]]
2020

2121
// 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.
2222

@@ -34,10 +34,10 @@ bubbleSortBasicAscending1 = (arr) => {
3434

3535
// console.log(bubbleSortBasicAscending1(array1));
3636

37-
// 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]
37+
//SOLUTION-2Now 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]
3838
// Learning JavaScript Data Structures and Algorithms-Loiane.pdf - Page - 225
3939

40-
bubbleSortBasicAscending2=arr=>{
40+
bubbleSortBasicAscending_2=arr=>{
4141

4242
for(leti=0;i<arr.length;i++){
4343
for(letj=0;j<arr.length-1;j++){
@@ -49,7 +49,7 @@ bubbleSortBasicAscending2 = arr => {
4949
returnarr;
5050
}
5151

52-
console.log(bubbleSortBasicAscending2(array1));
52+
//console.log(bubbleSortBasicAscending_2(array1));
5353

5454
bubbleSortBasicDescending=(arr)=>{
5555

@@ -65,39 +65,81 @@ bubbleSortBasicDescending = (arr) => {
6565

6666
// console.log(bubbleSortBasicDescending(array1));
6767

68-
/* SOLUTION - 2 - A more optimal solution, by reducing some of the loop execution.
68+
/* SOLUTION - 3 - A more optimal solution, by reducing some of the loop execution.
69+
6970
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
71+
72+
Remember the do-while syntax - the first do will execute anyway, but then the next while loop will ONLY execute if the condition of thw while is satisfied. In this case below, only after I find a mis-placed pair of elements, I flip 'swapped' to true. And only then while(swapped) gets satisfied > and only then the next while loop gets executed.
73+
7074
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.
7175
7276
And each time there's something to swap the do-while loop will continue, and the moment the whole array is sorted, and there's nothing more to sort the do-while loop will no more execute.
7377
*/
74-
bubbleSortAscending_Improved=arr=>{
7578

76-
// A flag that holds whether the swapping happened or not
77-
letswapped=false;
79+
bubbleSortAscending_Improved_3=arr=>{
80+
81+
// A flag that holds whether the swapping happened or not
82+
varswapped=false;
83+
84+
do{
85+
86+
swapped=false;
87+
88+
for(leti=0;i<arr.length-1;i++){
89+
90+
if((arr[i]&&arr[i+1])&&(arr[i]>arr[i+1])){
91+
92+
swap(arr,i,i+1);
93+
94+
// register the swap
95+
swapped=true;
96+
}
97+
}
98+
}while(swapped)
99+
100+
returnarr;
101+
}
102+
103+
// If there were no swaps then array is already sorted and there is no need to proceed.
104+
// The way I check the above condition is if - reversed of swapped is true, that is !swapped === true. And because after each actual swap, I am turning swapped=true, so (!swapped)===true above can only happen if there was no swapping.
105+
106+
console.log(bubbleSortAscending_Improved_3(array1))
107+
108+
109+
/* SOLUTION-4 - Same Implementation of the above with while loop - I need to loop continuously until a condition is met (no swaps happens i.e. 'swapped' is true).
110+
Note, in the below I am setting 'swapped' back to false after each swapping, because ONLY THEN THE NEXT ITERATION OF THE WHILE LOOP CAN GET EXECUTED. BECAUSE ONLY THEN (!swapped) will be true, and the while loop condition will be satisfied.
111+
112+
*/
113+
114+
bubbleSortAscending_Improved_4=arr=>{
115+
116+
varswapped=false;
117+
118+
while(!swapped){
78119

79-
do{
80-
swapped=false;
81-
for(leti=0;i<arr.length;i++){
82-
if(arr[i]&&arr[i+1]&&(arr[i]>arr[i+1])){
83-
swap(arr,i,i+1);
84120
swapped=true;
85-
}
121+
122+
arr.forEach((elem,index,array)=>{
123+
if(elem>array[index+1]){
124+
array[index]=array[index+1];
125+
array[index+1]=elem;
126+
swapped=false;
127+
}
128+
})
86129
}
87-
}while(swapped)
88-
returnarr;
130+
returnarr;
89131
}
90132

91-
console.log(bubbleSortAscending_Improved(array1));
133+
//console.log(bubbleSortAscending_Improved_4(array1))
92134

93-
/* SOLUTION-3 - Although complexity is still O(n^2). If we subtract the number of passes from the inner loop, we will avoid all the unnecessary comparisons made by the inner loop. So the idea is, for second nested loop when j = 1, I running it one less pass.
135+
/* SOLUTION-4 - Although complexity is still O(n^2). If we subtract the number of passes from the inner loop, we will avoid all the unnecessary comparisons made by the inner loop. So the idea is, for second nested loop when j = 1, I running it one less pass.
94136
95137
Even though we made this small change to improve the bubble sort algorithm a little bit, it is not a recommended algorithm. It has a complexity of O(n2).
96138
97139
Learning JavaScript Data Structures and Algorithms-Loiane.pdf - Page - 228
98140
*/
99141

100-
bubbleSortAscending_Improved_2=arr=>{
142+
bubbleSortAscending_Improved_4=arr=>{
101143
for(leti=0;i<arr.length;i++){
102144
for(j=0;j<arr.length-1-i;j++){
103145
if(arr[j]>arr[j+1]){
@@ -108,4 +150,4 @@ bubbleSortAscending_Improved_2 = arr => {
108150
returnarr;
109151
}
110152

111-
console.log(bubbleSortAscending_Improved_2(array1));
153+
//console.log(bubbleSortAscending_Improved_4(array1));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp