public class Main{ // bubble sort = pairs of adjacent elements are compared, and the elements // swapped if they are not in order. // Quadratic time O(n^2) // small data set = okay-ish // large data set = BAD (plz don't) public static void main(String[] args) { int array[] = {9, 1, 8, 2, 7, 3, 6, 4, 5}; bubbleSort(array); for(int i : array) { System.out.print(i); } } public static void bubbleSort(int array[]) { for(int i = 0; i < array.length - 1; i++) { for(int j = 0; j < array.length - i - 1; j++) { if(array[j] > array[j+1]) { int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } }
@andreamixvlog7478 Жыл бұрын
Hello, can i get this code?
@joyceasante8292 Жыл бұрын
Practicing... Ascending order public class Main { public static void main(String[] args) { int array[] = {7,3,2,1,4,0,8,6,5}; bubbleSort(array); for(int i: array){ System.out.print(i); } } public static void bubbleSort(int array[]){ for(int i = 0; i < array.length - 1; i++){ for(int j = 0; j < array.length - i -1; j++){ if(array[j] > array[j + 1]){ int temp = array[j]; array[j] = array[j+1]; array[j+1]= temp; } } } } } ************************* Descending order public class Main { public static void main(String[] args) { int array[] = {7,3,2,1,4,0,8,6,5}; bubbleSort(array); for(int i: array){ System.out.print(i); } } public static void bubbleSort(int array[]){ for(int i = 0; i < array.length - 1; i++){ for(int j = 0; j < array.length - i -1; j++){ if(array[j] < array[j + 1]){ int temp = array[j]; array[j] = array[j+1]; array[j+1]= temp; } } } } }
@koinahotahai11 ай бұрын
@@andreamixvlog7478yeah you can get it
@dhoneybeekingdom7889 Жыл бұрын
The comparison with the two cups was really intuitive! I wish someone had explained me variable swapping with that example.
@김우솔-j3l3 жыл бұрын
You are saving my life in cs classes. Thank you so much.
@BroCodez3 жыл бұрын
sweet! I'm glad the videos are helping!
@T-Rex0711 Жыл бұрын
Defeat the algorithm, Bros.
@jaykay84264 ай бұрын
Out of all the basic ways of sorting, this is the slowest of the basic ways to sort
@sakuriosky1573 Жыл бұрын
For the second for-loop for(int j = 0; j < array.length - i - 1; j++) Why must we subtract i and one? I understand why we need to subtract one but why i?
@jellojoy29779 ай бұрын
I know it's kind of late, but I'll reply anyway in case someone has the same question. We subtract i, so that way we won't check the numbers that are already sorted!
@pupper55803 ай бұрын
we subtract 1 so there wont be overflow from the array. You can avoid using -1 by doing the following steps: 1) start the j-loop from 1. 2) when you do comparison, you do arr[j-1] and arr[j] comparisons. Using this version of the loop might be more intuitive to understand.
@codewithschoolteacher5118Ай бұрын
In Bubble Sort, we compare pairs of numbers in the list and swap them if they are in the wrong order. After one complete pass through the list, the biggest number will be at the end, so we don't need to check it again in the next pass. Now, let's focus on this part of the loop: j < array.length - i - 1 What is j? j is the position in the array we are currently checking (the current pair of numbers being compared). What is array.length? array.length is the total number of elements in the array. For example, if the array has 6 numbers, array.length is 6. Why do we subtract 1? We subtract 1 because when we compare the element at j, we also compare it with the element at j + 1. If j were at the very end of the array, trying to check j + 1 would cause an error, because there’s no number after the last one. So, we stop one position early, by subtracting 1. Why subtract i? Every time we finish one pass through the array (one loop of the outer for loop), the largest number gets "bubbled up" to its correct position at the end of the array. In the second pass (i = 1), the second-largest number will be in its correct position. In the third pass (i = 2), the third-largest number will be in place, and so on. So, each time, we don’t need to check as many numbers as before because some of them are already sorted at the end. Subtracting i makes the loop shorter each time, so we don't waste time checking numbers that are already in the right place. Example with 6 Numbers: Imagine we have this array: {5, 2, 9, 1, 5, 6} (6 numbers). First pass (i = 0): We check and compare all numbers from position j = 0 to j = 4 (because array.length - i - 1 = 6 - 0 - 1 = 5). Second pass (i = 1): The largest number (9) is already in place, so we don’t need to compare it again. Now, we compare from j = 0 to j = 3 (because array.length - i - 1 = 6 - 1 - 1 = 4). Third pass (i = 2): The two largest numbers (9 and 6) are in place, so we only need to compare from j = 0 to j = 2. Each time, i gets bigger, and we check fewer numbers because the largest numbers are already sorted at the end!
@wave8060Ай бұрын
@@codewithschoolteacher5118thanks a lot
@moum.a81303 жыл бұрын
Bro you are just amazing , i really appriciate your works .. Please keep going
@BroCodez3 жыл бұрын
thanks for watching!
@Zippo_12342 жыл бұрын
These videos are great. Really helpful for my studying, thank you
@ashmikanln3110 Жыл бұрын
U r videos are my go to for all the sorting algos, Thank you for making these, Great help, Keep up the great work!
@poromoro24143 жыл бұрын
legit only channel I have notifications on for, lifesaver!
@BroCodez3 жыл бұрын
thanks Poromoro! I'm glad these videos are helping
@mohamedabdirizakahmed3539 Жыл бұрын
thank you so much, it makes me easy, cause I watched alot of tutorials but don't understand. yesterday I bought a C# course on udemy, the first video is talking how to make a Bubble sort, but i don't understand. then my mind said: go on yotube, may be you can find a good teacher that makes simply to this fucking Bubble sort. then i got you. Now i understand the Bubble sort. thank you so much..
@Snowmanver22 жыл бұрын
Bubble sort is a very easy algorithm to implement, but is quite inefficient with large data sets
@DetCoAnimeFan3 жыл бұрын
I know it already but I appreciate your effort. I currently know bubble sort, selection sort, insertion sort, binary and linear search as per my school syllabus. I will be waiting for your quick sort tutorial, I seriously want to learn it.
@BroCodez3 жыл бұрын
quicksort will be coming up soon I believe
@barelycodingtoday Жыл бұрын
I still come back to this video from time to time to rehash my BubbleSort. Thanks BRO!
@GamingPro-xn4hy Жыл бұрын
Bro is the best when it comes to explaining. I hope this guy blows up all over yt. I will support him
@leonardopensador8 ай бұрын
I'm watching your videos from Brazil and I want to thank you for taking the time to teach us. It was very didactic to understand your explanation. Thank you very much.
@aaronfernando5300 Жыл бұрын
Ey Brooooo ... i love you man ... not even my uni lecturer explained sorting this well .. saw this video 1 day prior to my exam and it helped meee soo much.. really appriciate BROOO ..Much Love from Sri Lanka ❤
@hather9938Ай бұрын
a lot better than uni teachers, thank you
@andres91086 ай бұрын
I'm from Colombia and I can understand all with this video, thx u (I love the manual example)
@wolfskind91353 жыл бұрын
You're really saving my ass studying for that computer science exam - thanks bro!
@ThEHUNtErCr97 ай бұрын
Really helpful video. I could not understand the concept of bubble sort before but now i do and it has helped me code my programs better thanks bro
@MohammadJavadd6 ай бұрын
Really can't be better than this, thanks so much it was great tutorial❤❤
@aditya_asundi3 жыл бұрын
you are great, just 15k subs away from the milestone! congrats
@BroCodez3 жыл бұрын
Yeah that's coming up soon!
@kyawsanaung20196 ай бұрын
Thank you for your video. I think you need to break the loop if the array is already sorted for efficiency.
@sabermmirza3 жыл бұрын
Everyone does not share information's easily and free, mostly want money, Thanks for sharing free INFORMATIONS BRO
@BroCodez3 жыл бұрын
no problem! Thanks for watching Saber
@thainguyenviet32673 жыл бұрын
always waiting for your video !!! keep doing this , love u
@BroCodez3 жыл бұрын
Thanks! I will continue
@bagnotneeded2 жыл бұрын
you are a truly gigachad... really golden video. keep going!
@aliiiccyee5736 Жыл бұрын
I love u!!!! Thank u for making the concept so clear n understandable💝
@autosuggestionasshole92083 жыл бұрын
the best channel on youtubeeee
@BroCodez3 жыл бұрын
Thanks bro!
@jdserrato3792 Жыл бұрын
I will savour this moment that does not involve recursion
@lambdadotjoburg7 ай бұрын
I am happy to announce that I will be building a complementary video series (Algorithmic Complexity Analysis) that analyzes many of the defined concepts within your talks to demonstrate it using a rigorous mathematical framework that describes algorithms on a meta-level and forms the perfect combination, when watched in conjunction with your video series
@xeahmedrefat2972 Жыл бұрын
Thanks from Egypt❤🔥
@BN-cr3el3 жыл бұрын
💯 thank you!
@junialdy Жыл бұрын
thank you for the video, very easy to understand with your explanation
@DanielSmith-uj7rr3 жыл бұрын
Thank You Bro! YOU ARE GREAT MY FRIEND! (People who dislikes are not from Computer Science background! LOL)! Well, I'm comfortable in Python programming. So, I didn't watch your Java code. But, your explanation is so perfect that it was building an easy understanding about the algorithm. So nicely explained! Bro, Do you also solve leet code problems?
@jerrypolemica2704 Жыл бұрын
Thanks from Italy!
@bluebaboon14 ай бұрын
all I keep hearing is bulbasaur 😆Thanks for the video!!
@qwikz53893 жыл бұрын
this helped me out thank you
@BroCodez3 жыл бұрын
Thanks for watching Qwikz!
@dharmeshajudiya1709 Жыл бұрын
Good explantation, Thanks for sharing
@MeditateRelaxEtcetera8 ай бұрын
You've got it sorted out.😎
@StefanosFragoulis Жыл бұрын
Great Video mate!
@sahinyusifli9756 Жыл бұрын
Thank you. It was so helpful!!
@codeammar Жыл бұрын
thx man for the amazing video , I wanted to note that since the inner loop has 8 elements to be compared and the last element won't get compared because the second element then will already be sorted, so for that the outerloop should be array.length - 2 right ?
@cashionsteven56953 жыл бұрын
holy moly, way better than my professor lol :))
@dankmemequeen9581 Жыл бұрын
Thank you very much bro, you put so much time and effort into your videos!
@AiGeneration-t2e2 жыл бұрын
Great explanation, thank you so much.
@plankalkulcompiler9468 Жыл бұрын
Very helpful video, thank you!
@aleacevedo1016 Жыл бұрын
As always... excellent videos! Thanks!
@geografixxxx Жыл бұрын
Very easy to understand, but I wish u made those tutorial in C.
@aleksandarduncevic9548 Жыл бұрын
Great videos, you are very helpfull! One question, in second for loop, can we write "int j = i" instead of 0, because we do not want to compare elements that are already sorted?
@poparobertandrei1698 Жыл бұрын
he already does that but its not the first i elements that are sorted its the last i
@johnhansen47943 жыл бұрын
Clear and Concise. Thanks.
@BroCodez3 жыл бұрын
Thanks for watching John!
@preet60952 жыл бұрын
Explanation of j< array.length-i-1 ? please...
@morhakak19072 жыл бұрын
After the first iteration the highest number will ended up in the highest position. so, in the next iteration we don't need to check again for that number (he's at the right position). After the second iteration the second highest number will ended up in the second highest position, so we don't need to check again for that number or the number in the highest position. And so on for the rest of the iterations.
@learner8005 Жыл бұрын
New bro here loved you video brooooo🤘🤘🤘
@abdulmoiz53113 жыл бұрын
Great video! Please explain QuickSort , Heap and Binary Trees!!
@BroCodez3 жыл бұрын
Thanks Abdul! We'll get to those topics eventually!
@jacklagos936 Жыл бұрын
Many thanks!
@MohitRaj-17123 жыл бұрын
you deserve more subscribers.
@BroCodez3 жыл бұрын
thanks Mohit!
@BruceNguyen19993 ай бұрын
Thank bro code you is my hero about progamming thanks bro
@iamfromasean7053 жыл бұрын
Thank you for the algorithm!
@BroCodez3 жыл бұрын
Thanks for commenting to help with the KZbin algorithm!
@iamfromasean7053 жыл бұрын
@@BroCodez Bro, can you do quick sort , please?
@canadianbeast86712 жыл бұрын
another simple implementation of bubble-sort algorithm public static void bubble(int[] arr) { int temp; for(int i=0; i
@sadman68529 ай бұрын
Thank you bro!
@muhammed__511 ай бұрын
very well done!
@Tomteluva827 ай бұрын
very useful, thanks!
@envektro25193 жыл бұрын
can you do this for every sorting algorithm? thanks in advance
@BroCodez3 жыл бұрын
I'll try
@pjguitar157 ай бұрын
for (int i = 0; i < array.length; i++) { for (int j = 0; j < array.length - 1; j++) { if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } This code worked for me. Why? My second for loop doesn't subtract i from array.length.
@majdkinani58710 ай бұрын
Me watching this as i have an exam tomorrow (im saved by this chad)
@MrLoser-ks2xn Жыл бұрын
Thanks!
@MaRvELsNERD10 ай бұрын
Bro is the goat 🐐
@nikitos24423 жыл бұрын
Like always...legendary!
@BroCodez3 жыл бұрын
Thanks Nikitos!
@yehannk4483 жыл бұрын
Bro thanks just what i was looking
@BroCodez3 жыл бұрын
awesome! Thanks for watching Yehan!
@alinaseerideas22953 жыл бұрын
Great job.. Please Start complete video series on node KS as back-end technology..
@BroCodez3 жыл бұрын
maybe! I'll let you guys vote on future topics when I release polls
@_4p_ Жыл бұрын
it was perfect. all of it
@youcefmantas4944 Жыл бұрын
Thanks Mate!!
@ALBERTJOAQUINENSELADA Жыл бұрын
thank you!
@kevinandgame4932Ай бұрын
You saved my grade bro
@anthonychiara680910 ай бұрын
why is it j
@elbioiseas73663 жыл бұрын
Good video explanation of the Bubble Sort algorithm. Would it be possible to make a video of binary tree algorithm with the different ways of visiting the tree structure plus how to insert, delete and search for a node in the tree? It would be very educational. Thank you.
@BroCodez3 жыл бұрын
I'm planning binary trees for a future video
@CSstudent_1001 Жыл бұрын
Thank you
@TITAN-sv5eg Жыл бұрын
thanks for clarify this
@vehanhemsara97933 жыл бұрын
Thank You
@BroCodez3 жыл бұрын
Thanks for watching Vehan!
@vehanhemsara97933 жыл бұрын
@@BroCodez 😊❤
@Omar-_-_2 жыл бұрын
Amazing, thanks alot
@admoonhermiz1 Жыл бұрын
thank you!!!
@YohannesBayehАй бұрын
So comparasion is always happens even if it is sorted
@YohannesBayehАй бұрын
Thank you bro😊
@jamiebenish15343 жыл бұрын
Hello brother :)
@qwikz53893 жыл бұрын
Hi
@BroCodez3 жыл бұрын
Hey Duckers!
@IsaacAwad.045 ай бұрын
thanks man
@amkiji29923 жыл бұрын
I Hope you can do a PHP cours i Really like the way that you teach you are amazing
@BroCodez3 жыл бұрын
maybe! I'll let you guys vote on future topics
@Saalltt15 күн бұрын
Love you bro❤
@lazyknight17433 ай бұрын
DROP a COMMENT for this awesome video.
@meetprotrash31783 жыл бұрын
I love you bro thanks 😘💙❤️😘
@BroCodez3 жыл бұрын
THanks bro!
@Cristian-me9id9 ай бұрын
thank you (:
@matinmonshizadeh2 жыл бұрын
thanks bro :D
@GhaziKhanShinwari23 күн бұрын
Thanks very much bro code, i'm really like you
@collynecity33312 жыл бұрын
Introduction for me😂😂😂😂 Ohhh yeah😾🎭🎭🎭🎭
@AlaEddineStudentMezdoud11 ай бұрын
great man
@ФайзуллаАсатуллаев Жыл бұрын
Thanks
@sanjays90823 жыл бұрын
Hey bro, Python data structures and algorithms would be awesome 🔥🔥
@BroCodez3 жыл бұрын
maybe in the future, most of my viewers wanted Java
@Poria_q9 ай бұрын
You're the best
@esnox9334 Жыл бұрын
Just because you asked, I'm commenting.
@sakiszintros77829 ай бұрын
hi.can you start from the end of the table to compare the elements or is it wrong?(Python)
@shreehari25893 жыл бұрын
Hey bro, can you start data structures and algorithms using python please?