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-kz2lu2 жыл бұрын
I wrote the similar solution. Was wondering will the BigO be O(n) with this recursion solution
@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.
@mindspray81642 жыл бұрын
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_Hoffman7 ай бұрын
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]))
@oscargm19795 ай бұрын
Superb
@YuriiKratser6 ай бұрын
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 Жыл бұрын
Holy cow how good the explanation is
@shrandesign4 ай бұрын
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
@nagendra16122 жыл бұрын
Thank you for the great content and your efforts.
@pradeepkumaresan-tr6qy9 ай бұрын
Hi bro , can also decrease the length of array for each passes once loop completed , it will also more efficient
@prachi_arora2 ай бұрын
Thank You
@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...
@djneils1002 жыл бұрын
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 Жыл бұрын
you don't see i++?
@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-uu9yv11 ай бұрын
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; } }
@JonathanKila2 жыл бұрын
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 }
@mhamadshebli2 жыл бұрын
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
@mhamadshebli2 жыл бұрын
* i meant nested loops ( not arrays ) :P
@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))
@DojoDyo7 ай бұрын
thank you sir
@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 Жыл бұрын
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());
@shujaatali841411 ай бұрын
This will failed for this array [8, 3, -1, 100, 20, -2, 4, -6]
@vokeolomu40852 жыл бұрын
Just wondering how the function returned nothing yet we got the result
@webdev10422 жыл бұрын
he did console log for the arr not the function
@austinjohn87132 жыл бұрын
i was wondering too until is noticed he first ran the function on the array which mutated it then console.log the arr after
@oscargm19792 жыл бұрын
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 Жыл бұрын
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; }
@parmaryash44062 жыл бұрын
Super
@rockedwow72172 жыл бұрын
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-qe3hd2 жыл бұрын
Nothing wrong! You just used while instead of do while! So the algorithm remains same
@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-js2 жыл бұрын
Hello Sir, the font size is too much small , it is very hard to read on small sized devices...
@aaa1820-g4g Жыл бұрын
I used to do this with an even less optimized solution by using nested for loop
@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-intellect2 жыл бұрын
you did not explain why swap = false or true.
@an_on_nym2 жыл бұрын
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 Жыл бұрын
why is your code running without a return
@ivan448611 ай бұрын
because he mutates the original arr
@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 Жыл бұрын
🎉
@anandrajagopal19412 жыл бұрын
Is it fine if we use in-build sort command let sort = array.sort((a,b) => a - b);
@rafeeqshaik73552 жыл бұрын
Use it in production. This is just a algorithms course 😃