Learn Bubble Sort in 7 minutes 🤿

Поділитися
Вставка
  • Опубліковано 19 гру 2024

КОМЕНТАРІ • 243

  • @BroCodez
    @BroCodez  3 роки тому +147

    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
      @andreamixvlog7478 Рік тому +1

      Hello, can i get this code?

    • @joyceasante8292
      @joyceasante8292 Рік тому +5

      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;
      }
      }
      }
      }
      }

    • @koinahotahai
      @koinahotahai 11 місяців тому

      ​@@andreamixvlog7478yeah you can get it

    • @soumyajitpaul6135
      @soumyajitpaul6135 17 днів тому

      Thank u bro you made my day 🗿(Even though I am posting this comment at night)
      You are one of the best computer teacher that anyone can get🗿

  • @dhoneybeekingdom7889
    @dhoneybeekingdom7889 Рік тому +35

    The comparison with the two cups was really intuitive! I wish someone had explained me variable swapping with that example.

  • @김우솔-j3l
    @김우솔-j3l 3 роки тому +26

    You are saving my life in cs classes. Thank you so much.

    • @BroCodez
      @BroCodez  3 роки тому +8

      sweet! I'm glad the videos are helping!

  • @T-Rex0711
    @T-Rex0711 Рік тому +22

    Defeat the algorithm, Bros.

  • @sakuriosky1573
    @sakuriosky1573 Рік тому +5

    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?

    • @jellojoy2977
      @jellojoy2977 10 місяців тому +5

      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!

    • @pupper5580
      @pupper5580 4 місяці тому +2

      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
      @codewithschoolteacher5118 2 місяці тому +10

      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
      @wave8060 Місяць тому

      @@codewithschoolteacher5118thanks a lot

  • @moum.a8130
    @moum.a8130 3 роки тому +35

    Bro you are just amazing , i really appriciate your works .. Please keep going

    • @BroCodez
      @BroCodez  3 роки тому +10

      thanks for watching!

  • @siddhartharofficial8360
    @siddhartharofficial8360 7 днів тому

    These videos are insanely helpful at any level. The way that u help me visualize how the sort works at the start of the video is great. Thanks A bunch!!

  • @poromoro2414
    @poromoro2414 3 роки тому +4

    legit only channel I have notifications on for, lifesaver!

    • @BroCodez
      @BroCodez  3 роки тому

      thanks Poromoro! I'm glad these videos are helping

  • @ashmikanln3110
    @ashmikanln3110 Рік тому +4

    U r videos are my go to for all the sorting algos, Thank you for making these, Great help, Keep up the great work!

  • @jaykay8426
    @jaykay8426 5 місяців тому +7

    Out of all the basic ways of sorting, this is the slowest of the basic ways to sort

  • @Zippo_1234
    @Zippo_1234 2 роки тому +11

    These videos are great. Really helpful for my studying, thank you

  • @leonardopensador
    @leonardopensador 9 місяців тому

    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.

  • @GamingPro-xn4hy
    @GamingPro-xn4hy 2 роки тому +1

    Bro is the best when it comes to explaining. I hope this guy blows up all over yt. I will support him

  • @Snowmanver2
    @Snowmanver2 2 роки тому +18

    Bubble sort is a very easy algorithm to implement, but is quite inefficient with large data sets

  • @barelycodingtoday
    @barelycodingtoday Рік тому

    I still come back to this video from time to time to rehash my BubbleSort.
    Thanks BRO!

  • @DetCoAnimeFan
    @DetCoAnimeFan 3 роки тому +21

    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.

    • @BroCodez
      @BroCodez  3 роки тому +13

      quicksort will be coming up soon I believe

  • @aditya_asundi
    @aditya_asundi 3 роки тому +1

    you are great, just 15k subs away from the milestone! congrats

    • @BroCodez
      @BroCodez  3 роки тому +2

      Yeah that's coming up soon!

  • @autosuggestionasshole9208
    @autosuggestionasshole9208 3 роки тому

    the best channel on youtubeeee

  • @thainguyenviet3267
    @thainguyenviet3267 3 роки тому

    always waiting for your video !!! keep doing this , love u

    • @BroCodez
      @BroCodez  3 роки тому

      Thanks! I will continue

  • @bagnotneeded
    @bagnotneeded 2 роки тому

    you are a truly gigachad... really golden video. keep going!

  • @andres9108
    @andres9108 7 місяців тому

    I'm from Colombia and I can understand all with this video, thx u (I love the manual example)

  • @hather9938
    @hather9938 2 місяці тому

    a lot better than uni teachers, thank you

  • @StefanosFragoulis
    @StefanosFragoulis Рік тому

    Great Video mate!

  • @ThEHUNtErCr9
    @ThEHUNtErCr9 8 місяців тому

    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

  • @xeahmedrefat2972
    @xeahmedrefat2972 Рік тому

    Thanks from Egypt❤‍🔥

  • @OweYaOne
    @OweYaOne 21 день тому

    Thanks again, bro :)

  • @jerrypolemica2704
    @jerrypolemica2704 Рік тому

    Thanks from Italy!

  • @qwikz5389
    @qwikz5389 3 роки тому +1

    this helped me out thank you

    • @BroCodez
      @BroCodez  3 роки тому +1

      Thanks for watching Qwikz!

  • @BN-cr3el
    @BN-cr3el 3 роки тому +2

    💯 thank you!

  • @dharmeshajudiya1709
    @dharmeshajudiya1709 Рік тому

    Good explantation, Thanks for sharing

  • @MohammadJavadd
    @MohammadJavadd 6 місяців тому

    Really can't be better than this, thanks so much it was great tutorial❤❤

  • @learner8005
    @learner8005 Рік тому

    New bro here loved you video brooooo🤘🤘🤘

  • @aaronfernando5300
    @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 ❤

  • @sabermmirza
    @sabermmirza 3 роки тому

    Everyone does not share information's easily and free, mostly want money, Thanks for sharing free INFORMATIONS BRO

    • @BroCodez
      @BroCodez  3 роки тому

      no problem! Thanks for watching Saber

  • @aliiiccyee5736
    @aliiiccyee5736 Рік тому

    I love u!!!! Thank u for making the concept so clear n understandable💝

  • @aleacevedo1016
    @aleacevedo1016 Рік тому

    As always... excellent videos! Thanks!

  • @sahinyusifli9756
    @sahinyusifli9756 Рік тому

    Thank you. It was so helpful!!

  • @BruceNguyen1999
    @BruceNguyen1999 4 місяці тому

    Thank bro code you is my hero about progamming thanks bro

  • @wolfskind9135
    @wolfskind9135 3 роки тому

    You're really saving my ass studying for that computer science exam - thanks bro!

  • @MohitRaj-1712
    @MohitRaj-1712 3 роки тому

    you deserve more subscribers.

  • @junialdy
    @junialdy Рік тому

    thank you for the video, very easy to understand with your explanation

  • @kyawsanaung2019
    @kyawsanaung2019 6 місяців тому

    Thank you for your video. I think you need to break the loop if the array is already sorted for efficiency.

  • @canadianbeast8671
    @canadianbeast8671 2 роки тому

    another simple implementation of bubble-sort algorithm
    public static void bubble(int[] arr) {
    int temp;
    for(int i=0; i

  • @plankalkulcompiler9468
    @plankalkulcompiler9468 Рік тому

    Very helpful video, thank you!

  • @sadman6852
    @sadman6852 9 місяців тому

    Thank you bro!

  • @lambdadotjoburg
    @lambdadotjoburg 8 місяців тому

    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

  • @cashionsteven5695
    @cashionsteven5695 3 роки тому

    holy moly, way better than my professor lol :))

  • @dankmemequeen9581
    @dankmemequeen9581 Рік тому

    Thank you very much bro, you put so much time and effort into your videos!

  • @Tomteluva82
    @Tomteluva82 8 місяців тому

    very useful, thanks!

  • @AiGeneration-t2e
    @AiGeneration-t2e 2 роки тому

    Great explanation, thank you so much.

  • @muhammed__5
    @muhammed__5 11 місяців тому

    very well done!

  • @iamfromasean705
    @iamfromasean705 3 роки тому +1

    Thank you for the algorithm!

    • @BroCodez
      @BroCodez  3 роки тому +1

      Thanks for commenting to help with the UA-cam algorithm!

    • @iamfromasean705
      @iamfromasean705 3 роки тому

      @@BroCodez Bro, can you do quick sort , please?

  • @preet6095
    @preet6095 2 роки тому

    Explanation of j< array.length-i-1 ? please...

    • @morhakak1907
      @morhakak1907 2 роки тому +1

      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.

  • @jdserrato3792
    @jdserrato3792 Рік тому

    I will savour this moment that does not involve recursion

  • @envektro2519
    @envektro2519 3 роки тому +2

    can you do this for every sorting algorithm? thanks in advance

  • @jacklagos936
    @jacklagos936 Рік тому

    Many thanks!

  • @yehannk448
    @yehannk448 3 роки тому

    Bro thanks just what i was looking

    • @BroCodez
      @BroCodez  3 роки тому

      awesome! Thanks for watching Yehan!

  • @MeditateRelaxEtcetera
    @MeditateRelaxEtcetera 9 місяців тому

    You've got it sorted out.😎

  • @youcefmantas4944
    @youcefmantas4944 Рік тому

    Thanks Mate!!

  • @johnhansen4794
    @johnhansen4794 3 роки тому

    Clear and Concise. Thanks.

    • @BroCodez
      @BroCodez  3 роки тому

      Thanks for watching John!

  • @DanielSmith-uj7rr
    @DanielSmith-uj7rr 3 роки тому +3

    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?

  • @vehanhemsara9793
    @vehanhemsara9793 3 роки тому +1

    Thank You

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Рік тому

    Thanks!

  • @Saalltt
    @Saalltt Місяць тому

    Love you bro❤

  • @nikitos2442
    @nikitos2442 3 роки тому

    Like always...legendary!

  • @_4p_
    @_4p_ Рік тому

    it was perfect.
    all of it

  • @Math_kru_earng
    @Math_kru_earng 17 днів тому

    thanks!

  • @Omar-_-_
    @Omar-_-_ 2 роки тому

    Amazing, thanks alot

  • @bluebaboon1
    @bluebaboon1 4 місяці тому

    all I keep hearing is bulbasaur 😆Thanks for the video!!

  • @MaRvELsNERD
    @MaRvELsNERD 10 місяців тому

    Bro is the goat 🐐

  • @ALBERTJOAQUINENSELADA
    @ALBERTJOAQUINENSELADA Рік тому

    thank you!

  • @alinaseerideas2295
    @alinaseerideas2295 3 роки тому

    Great job..
    Please Start complete video series on node KS as back-end technology..

    • @BroCodez
      @BroCodez  3 роки тому

      maybe! I'll let you guys vote on future topics when I release polls

  • @abdulmoiz5311
    @abdulmoiz5311 3 роки тому +1

    Great video! Please explain QuickSort , Heap and Binary Trees!!

    • @BroCodez
      @BroCodez  3 роки тому +3

      Thanks Abdul! We'll get to those topics eventually!

  • @admoonhermiz1
    @admoonhermiz1 Рік тому

    thank you!!!

  • @TITAN-sv5eg
    @TITAN-sv5eg Рік тому

    thanks for clarify this

  • @IsaacAwad.04
    @IsaacAwad.04 6 місяців тому

    thanks man

  • @pjguitar15
    @pjguitar15 8 місяців тому

    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.

  • @joelpose7269
    @joelpose7269 Рік тому +1

    i dont understand the "J" loop, why do you substract "i"? could someone explain pls

  • @aleksandarduncevic9548
    @aleksandarduncevic9548 Рік тому +1

    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
      @poparobertandrei1698 Рік тому

      he already does that
      but its not the first i elements that are sorted
      its the last i

  • @geografixxxx
    @geografixxxx Рік тому

    Very easy to understand, but I wish u made those tutorial in C.

  • @lazyknight1743
    @lazyknight1743 4 місяці тому

    DROP a COMMENT for this awesome video.

  • @anthonychiara6809
    @anthonychiara6809 10 місяців тому

    why is it j

  • @majdkinani587
    @majdkinani587 10 місяців тому

    Me watching this as i have an exam tomorrow (im saved by this chad)

  • @codeammar
    @codeammar Рік тому +1

    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 ?

  • @matinmonshizadeh
    @matinmonshizadeh 2 роки тому

    thanks bro :D

  • @GhaziKhanShinwari
    @GhaziKhanShinwari Місяць тому

    Thanks very much bro code, i'm really like you

  • @CSstudent_1001
    @CSstudent_1001 Рік тому

    Thank you

  • @Cristian-me9id
    @Cristian-me9id 10 місяців тому

    thank you (:

  • @jamiebenish1534
    @jamiebenish1534 3 роки тому +3

    Hello brother :)

  • @noah77
    @noah77 3 роки тому +1

    Ayy!! It's me

    • @BroCodez
      @BroCodez  3 роки тому

      Moonlight!

    • @noah77
      @noah77 3 роки тому

      @@BroCodez heheheh, hewo!

  • @AlaEddineStudentMezdoud
    @AlaEddineStudentMezdoud Рік тому

    great man

  • @Dumb4Creepy
    @Dumb4Creepy Рік тому

    can someone help me... why is it that we use " i < array.length - 1 " coz "

  • @meetprotrash3178
    @meetprotrash3178 3 роки тому +1

    I love you bro thanks 😘💙❤️😘

  • @LoveTaiwan678
    @LoveTaiwan678 3 роки тому

    Thank you ;)

    • @BroCodez
      @BroCodez  3 роки тому +1

      thanks for watching Yu!

  • @gabrielcasanova7034
    @gabrielcasanova7034 2 роки тому +1

    Hey! bro or guys, why are u using a nested loop? still didnt get it

  • @wolanus
    @wolanus 3 роки тому

    Great video

  • @collynecity3331
    @collynecity3331 2 роки тому +1

    Introduction for me😂😂😂😂
    Ohhh yeah😾🎭🎭🎭🎭

  • @embodormo7398
    @embodormo7398 3 роки тому

    Thanks for video

    • @BroCodez
      @BroCodez  3 роки тому

      np! Thanks for watching EMbo!

  • @amr19932012
    @amr19932012 2 роки тому

    why making the second loop j

    • @mehdimuhebbi2456
      @mehdimuhebbi2456 2 роки тому

      i was woundering the same thing...

    • @chaoticxie1425
      @chaoticxie1425 Рік тому

      figure it out? I was wondering the same

    • @fen-fi
      @fen-fi Рік тому +1

      The n - i - 1 expression in the inner for loop of the bubbleSort method is used to reduce the number of iterations of the inner loop as the array gets partially sorted.
      Each pass of the outer loop places the largest element at the end of the array, so in the next pass, there's no need to compare that element again. This is why i is subtracted from n in the condition of the inner loop. This way, the inner loop only needs to iterate over the unsorted part of the array.
      By reducing the number of comparisons, the algorithm's efficiency is improved and the sorting process is faster.

  • @mohamedabdirizakahmed3539
    @mohamedabdirizakahmed3539 Рік тому +1

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