JavaScript Algorithms - 21 - Bubble Sort Solution

  Рет қаралды 47,799

Codevolution

Codevolution

Күн бұрын

Пікірлер: 49
@HariPrasad-qe3hd
@HariPrasad-qe3hd 2 жыл бұрын
Bubble sort in Recursion: function BubbleSort(arr, swapped=false){ swapped = false; for(let i = 0; i < arr.length -1;i++){ if(arr[i] > arr[i+1]){ let temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; swapped = true } } if(!swapped){ return arr; } else { return BubbleSort(arr,swapped) } }
@AnilKanojiya-kz2lu
@AnilKanojiya-kz2lu 2 жыл бұрын
I wrote the similar solution. Was wondering will the BigO be O(n) with this recursion solution
@Shaktish-kumar
@Shaktish-kumar Жыл бұрын
Though recursive approach makes the code readable and looks simple but it has its downsides, Recursion in JavaScript can lead to stack overflow errors for very large arrays because each recursive call consumes stack space. This limits the size of the array that can be sorted using this approach.
@mindspray8164
@mindspray8164 2 жыл бұрын
As a beginner it felt so good to come to this video with the solution I made in the last video when you asked us to try first, and see that I had nearly everything exactly similar, down to the variable names, save for a check I had to see if the last element was empty rather than stopping the loop at arr.length-1. Thank you so much for this course and your clear teaching style.
@Herman_Hoffman
@Herman_Hoffman 7 ай бұрын
I improved the efficiency by accounting for how this algorithm sorts right to left so to speak. There isn't a need to always compare until the end of the array every pass since we're sorting one number at a time essentially. const bubbleSort = arr => { let swapped; let j = 0; do { swapped = false; for (let i = 0; i < arr.length - j - 1; i++) { if (arr[i] > arr[i + 1]) { let temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = true; } } j++; } while (swapped) return arr; } console.log(bubbleSort([8, 20, -2, 4, -6]))
@oscargm1979
@oscargm1979 5 ай бұрын
Superb
@YuriiKratser
@YuriiKratser 6 ай бұрын
Good job, thanks! The intresting fact, if you ask chatGPT or Cloud 3.5 generate the Bubble Sort algo, they will do it without the flag var but it's inefficient.
@kristijanlazarev
@kristijanlazarev Жыл бұрын
Holy cow how good the explanation is
@shrandesign
@shrandesign 4 ай бұрын
Just use a while loop and have a boolean check to see if any element is sorted at the end, if true then iterate again else return arr
@nagendra1612
@nagendra1612 2 жыл бұрын
Thank you for the great content and your efforts.
@pradeepkumaresan-tr6qy
@pradeepkumaresan-tr6qy 9 ай бұрын
Hi bro , can also decrease the length of array for each passes once loop completed , it will also more efficient
@prachi_arora
@prachi_arora 2 ай бұрын
Thank You
@navuyi3243
@navuyi3243 Жыл бұрын
There is a better, more optimised implementation to the bubble sort algorithm with iterating from the back of the array to beginning - after first iteration of the loop we are sure that smallest element is on the 0th index. After second iteration second smallest is on 1 and so on...
@djneils100
@djneils100 2 жыл бұрын
there is an optimisation you could do by making the -1 in the for loop a variable and increasing it by 1 every time you do a pass
@Kieranp96
@Kieranp96 Жыл бұрын
you don't see i++?
@djneils100
@djneils100 Жыл бұрын
I mean in the for loop there is arr.length-1 This is ok for the first pass but next time it could be arr.length-2 and the next time arr.length-3 since each pass puts the highest value in it's correct and final place. So if you started with arr.length-x and made x initially 1 then you could add 1 to x each time round and make the algorithm a bit more efficient. let x=1 for(let I=0;iarr[i+1]) swap items x++ } @@Kieranp96
@Ayoubmajid-uu9yv
@Ayoubmajid-uu9yv 11 ай бұрын
there is an extra optimisation in each for loop the last element will be sorted you don't need to check it again so the best code is : function swap(v1, v2) { return [v2, v1]; } // big-O=O(n^2) function bubbleSort(arr) { let isSwapped = false; for (let i = 0; i arr[j + 1]) { isSwap = true; [arr[j], arr[j + 1]] = swap(arr[j], arr[j + 1]); } } if (!isSwapped) return; } }
@JonathanKila
@JonathanKila 2 жыл бұрын
My solution was different. Not sure if it's as efficient: const bubbleSort = arr => { for (let j = 0; j < arr.length; j++) { for (let i = 0; i < arr.length; i++) { if (arr[i] > arr[i + 1]) { const temp = arr[i] arr[i] = arr[i + 1] arr[i + 1] = temp } } } return arr }
@mhamadshebli
@mhamadshebli 2 жыл бұрын
you made the J loop but did not used it !! just replace arr[ i ] with arr[ j ] and replace arr[i + 1] with arr[ i ] also... u defined both of the j and i loops to start and end from the same position !! and I'm afraid that's not right u have to start the positions of j loop at 0 and i loop at j + 1 so u now have two pointers in front of each other hope that I explained it clearly ... but here look at the way your code must look like const bubbleSort = (arr) => { if (arr === null) return false; for (let j = 0; j < arr.length - 1; j++) { for (let i = j + 1; i < arr.length; i++) { if (arr[ j ] > arr[ i ]) { let temp = arr[ i ]; arr[ i ] = arr[ j ]; arr[ j ] = temp; } } } return arr; }; but ofc the solution in the vid has better time complexity since he used one array instead of 2 nested arrays
@mhamadshebli
@mhamadshebli 2 жыл бұрын
* i meant nested loops ( not arrays ) :P
@rishabh2k01
@rishabh2k01 Жыл бұрын
your method is batter than mine let array = [ 5, 2 , 1 ,-6 , 8] function bubble(arr){ for(let i =0; i < arr.length; i++){ for(let j= i +1; j < arr.length; j++){ if(arr[i] > arr[j]){ let temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } return arr } console.log(bubble(array))
@DojoDyo
@DojoDyo 7 ай бұрын
thank you sir
@armoredchimp
@armoredchimp Жыл бұрын
Here's mine, it works I think but definitely isn't efficient/ has more lines of code than necessary: function bubble(arr) { let sorted = 0; function sort(arr) { sorted = 0; for (let i = 0; i < arr.length; i++) { if (arr[i] > arr[i + 1]) { sorted = 1; let a = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = a; } } return arr; } do { sort(arr); } while (sorted > 0); console.log(arr); return arr; }
@dokhangtseringnamgyal8735
@dokhangtseringnamgyal8735 Жыл бұрын
Do we have to use do while loop ?because below code also returns in sorted order :- function bubble(){ const array = [-5,-2,20,4,6,10] for(let i=0;iarray[i+1]){ const temp = array[i] array[i] = array[i+1] array[i+1] = temp } } return array } console.log(bubble());
@shujaatali8414
@shujaatali8414 11 ай бұрын
This will failed for this array [8, 3, -1, 100, 20, -2, 4, -6]
@vokeolomu4085
@vokeolomu4085 2 жыл бұрын
Just wondering how the function returned nothing yet we got the result
@webdev1042
@webdev1042 2 жыл бұрын
he did console log for the arr not the function
@austinjohn8713
@austinjohn8713 2 жыл бұрын
i was wondering too until is noticed he first ran the function on the array which mutated it then console.log the arr after
@oscargm1979
@oscargm1979 2 жыл бұрын
My ugly solution () : function bubbleSort(arr){ let elementsToSwap = true; while(elementsToSwap){ for(let i = 1; i < arr.length;++i){ if(arr[i-1] < arr[i]){ elementsToSwap = false; }else if(arr[i-1] > arr[i]){ elementsToSwap = true; break; } } for(let i = 1; i < arr.length;++i){ if(arr[i-1] > arr[i]){ let temp = arr[i-1]; arr[i-1] = arr[i] arr[i] = temp } } } return arr; }
@richardtbohnen5070
@richardtbohnen5070 Жыл бұрын
Interesting way to write the if statement: if (arr[i] > arr[i + 1]) { [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; swapped = true; }
@parmaryash4406
@parmaryash4406 2 жыл бұрын
Super
@rockedwow7217
@rockedwow7217 2 жыл бұрын
is this method wrong: function bubbleSort(arr){ let swapped = true while(swapped) { swapped = false for(let i = 0; i < arr.length; i++){ let temporary= arr[i] if(arr[i] > arr[i + 1]){ arr[i] = arr[i + 1] arr[i + 1 ] = temporary swapped = true } } } return arr } console.log(bubbleSort([-2, -4, 9 , 2,9, -19, 4]))
@HariPrasad-qe3hd
@HariPrasad-qe3hd 2 жыл бұрын
Nothing wrong! You just used while instead of do while! So the algorithm remains same
@Xraxus_
@Xraxus_ Жыл бұрын
I used recursion function bubbleSort(arr, elementsSwapped = false) { for (let i = 0; i < arr.length - 1; i++) { if (arr[i] > arr[i + 1]) { [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]]; elementsSwapped = true; } } if (!elementsSwapped) return arr; return bubbleSort(arr); } console.log(bubbleSort([-5, 50, -3, 25, 0, 2, -5, -5, -5,25,44,34, -2, -2, -2]));
@codingconcepts-js
@codingconcepts-js 2 жыл бұрын
Hello Sir, the font size is too much small , it is very hard to read on small sized devices...
@aaa1820-g4g
@aaa1820-g4g Жыл бұрын
I used to do this with an even less optimized solution by using nested for loop
@srudsalam6970
@srudsalam6970 Жыл бұрын
twice faster compare to the one of while loop function sort(arr) { for (let i = 0; i < arr.length - 1; i++) { for (let j = i; j >= 0; j--) { if (arr[j] > arr[j + 1]) { [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]; } } } return arr; }
@soulful-intellect
@soulful-intellect 2 жыл бұрын
you did not explain why swap = false or true.
@an_on_nym
@an_on_nym 2 жыл бұрын
If it swaps means still array is not sorted and it should run the loop but when it does not swap it means array is sorted and loop should stop.
@carlosmetaute4106
@carlosmetaute4106 Жыл бұрын
why is your code running without a return
@ivan4486
@ivan4486 11 ай бұрын
because he mutates the original arr
@BlissAden
@BlissAden Жыл бұрын
My solution was slighlty different dont know how efficent it is: function bubbleSort(arr) { let cleanCount = 0; while (cleanCount !== arr.length -1) { for (let i = 1; i < arr.length; i++) { if (arr[i - 1]
@raghavenderreddy2729
@raghavenderreddy2729 Жыл бұрын
🎉
@anandrajagopal1941
@anandrajagopal1941 2 жыл бұрын
Is it fine if we use in-build sort command let sort = array.sort((a,b) => a - b);
@rafeeqshaik7355
@rafeeqshaik7355 2 жыл бұрын
Use it in production. This is just a algorithms course 😃
@eelguneezmemmedov1671
@eelguneezmemmedov1671 2 жыл бұрын
You so smart!
@Justinbieber4eva
@Justinbieber4eva 2 жыл бұрын
Can you make a new react course using hooks?
JavaScript Algorithms - 22 - Insertion Sort
5:48
Codevolution
Рет қаралды 40 М.
5 JavaScript Concepts You HAVE TO KNOW
9:38
James Q Quick
Рет қаралды 1,4 МЛН
Каха и дочка
00:28
К-Media
Рет қаралды 3,4 МЛН
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
Bubble Sort Algorithm Tutorial in Java - How Fast Is It?
11:33
Coding with John
Рет қаралды 79 М.
Bubble Sort in Plain English
11:35
Programming with Mosh
Рет қаралды 173 М.
JavaScript Algorithms - 20 - Bubble Sort
4:47
Codevolution
Рет қаралды 38 М.
Sorting Algorithms in JS : Bubble Sort
11:16
getMaxed
Рет қаралды 6 М.
The Black Box Method: How to Learn Hard Concepts Quickly
14:09
Colin Galen
Рет қаралды 1,1 МЛН
Learn JavaScript SORTING in 6 minutes! 🗃
6:42
Bro Code
Рет қаралды 34 М.
Sorting Algorithms Explained Visually
9:01
Beyond Fireship
Рет қаралды 563 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН