Bubble sort in 2 minutes

Поділитися
Вставка
  • Опубліковано 7 січ 2025

КОМЕНТАРІ • 281

  • @istiaqahmed8309
    @istiaqahmed8309 4 роки тому +174

    After completing a 30 minutes lecture on the topic this video is like a dessert after dinner.

  • @SzechSauce
    @SzechSauce 5 років тому +487

    These videos are great, 30mins of my lecture summarized in 2mins lol

    • @pxolqopt3597
      @pxolqopt3597 4 роки тому +13

      30 minutes to explain bubble sort? Really?

    • @prometheus-naturalscience9371
      @prometheus-naturalscience9371 3 роки тому +25

      @@mtosity Usually a CS lecture on something like this would also include proofs for correctness, and time and memory complexity. Dont think it adds up to 30 minutes, but its obviously a lot more than what is shown here, which basically just the idea of the algorithm and an example.

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

      True😂

    • @AnkurSingh-uc7gz
      @AnkurSingh-uc7gz 2 роки тому

      In min😂 2X

    • @100_focus
      @100_focus 2 роки тому

      wait u guys are learning this in uni???

  • @realwizardry834
    @realwizardry834 6 років тому +428

    Love this visual way of learning. Thank you for the work put into this

  • @r0btaylor
    @r0btaylor 2 роки тому +72

    I only recently found your channel and I'm finding your style and clarity to be especially effective for my learning. Just wanted to let you know how much I've been appreciating your content. Thanks!

    • @MichaelSambol
      @MichaelSambol  2 роки тому +13

      I appreciate you watching, thank you!

  • @kewtomrao
    @kewtomrao 3 роки тому +29

    His two minutes of silent teaching was better than an hour of our lecturer's class.

  • @lukebrown2498
    @lukebrown2498 Рік тому +16

    Hello, i'm a student from the UK and my course requires me to learn a lot of the sorting algorithms that you have shown. Usually, only resources from people specifically covering the course is helpful, but these visual aids have been really effective and engaging, so thanks so much

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

      glad the videos helped! thanks for the note.

  • @ronitmehta2594
    @ronitmehta2594 4 роки тому +142

    I am a visual learner so I found this extremely helpful!

  • @omersakkar5670
    @omersakkar5670 6 місяців тому +4

    Thanks for the explination. I wanted to point out that you can right much more effecient code:
    for i = 0 to n-1
    for j = 0 to n-i-2
    if array[j] > array[j+1]
    swap(array[j], array[j+1])

  • @Krokodil986
    @Krokodil986 5 років тому +22

    amazing video - short, precise, and explains very well

    • @eeevoo
      @eeevoo 4 роки тому

      @@Raum_Raum_Raum_Raum damn why so toxic

  • @ichoosemeimsorry
    @ichoosemeimsorry Рік тому +3

    lmaoooo i cant believe i managed to understand what i have been struggling with the whole semester in 2 minutes. thanks man

  • @ravs
    @ravs 6 років тому +26

    Very simple and concise way to explain this algorithm. Keep up the good work and looking forward to more videos.

  • @valvarexart
    @valvarexart 8 років тому +40

    Very nice and to-the-point videos, thank you! A small note: bubble sort would perform better if n was decreased after each iteration (reflecting the fact that the last element is in its final position) and if we keep track of whether swaps have been made or not during a round (if not, we can just terminate).

    • @stefanjovanic7725
      @stefanjovanic7725 5 років тому +3

      I was looking for this explanation that's how simple it is for some people. Two sentences to explain what is going on.

    • @shalev1234
      @shalev1234 5 років тому

      And I think also every even iteration a bubble of small elements to the left will be good for a case that a small element is on the right side of the array.

    • @jliu66-p1q
      @jliu66-p1q 2 роки тому +1

      the best optimization is setting your outter loop stop index to be the last swapped index of your inner loop. This makes it more naturally adaptive than keeping track of a swaps made boolean

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

    Thanks, I cleared my exam easily by watching this

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

    now THIS is the best bubble sort guide ever

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

    Thanks for a clear and simple video! It's Helping for my GCSE this May. Some people just overcomplicate these sort methods.

  • @NB20LOL
    @NB20LOL 3 місяці тому +1

    Very helpful, have a test in 2 days and know almost everything on this topic

  • @chiragshilwant886
    @chiragshilwant886 5 років тому +13

    According to your code, Number of comparisons is n(n-1) but it can be reduced to n(n-1)/2 by setting j upto n-i-1

  • @juicetime222
    @juicetime222 10 місяців тому +1

    I have an exam soon, this video really helped!

  • @hydhexane
    @hydhexane Рік тому +3

    i have exam tomorrow, this video helped me revise bubble , thanks

  • @陳致揚
    @陳致揚 7 місяців тому

    I have watched your all videos several times whenever i need to review the algorithm, " You Are Definitely A God Of Algorithm"

  • @luke9524
    @luke9524 5 років тому +3

    Incredible video, you're style of teaching is very easy to understand.

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

    Best video I've seen to explain bubble sort, thank you!

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

    you're videos are more helpful than other videos that are 30 to 40min long and yet at the end still are confused.

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

    This animation absolutely made me understand the bubble sort

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

    Thankyou from saving me from watching 1 hour lecture, when I am in rush before exams

  • @HarunAltay
    @HarunAltay 7 років тому +18

    Thanks Michael. Great job! But I guess pseudu code is wrong. It may be:
    for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array.length - 1 - i; j++) {
    if (less(array[j + 1], array[j]))
    exchange(array, j, j + 1);
    }
    }

    • @piyushsahoo3636
      @piyushsahoo3636 Рік тому +2

      yes both are correct but yours is more optimized

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

    Thanks! No music and no trash!

  • @yikayiyo2920
    @yikayiyo2920 6 років тому +14

    for i from 1 to N: # N-1 iterations to sort a N-length list
    for j from 0 to N-i: # after i iterations ,the last i elements are sored

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

    Best algo channel on youtube.

  • @nicolasrodriguez1499
    @nicolasrodriguez1499 4 роки тому

    Thanks dude, this shorts videos are saving me for my algoritm exam!
    Thanks!

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

    Bruh is this really what took my professor most of the class trying to explain😭 thank you so much for this

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

    I literally coded it after u explaining it to me. This helped soo much!

  • @Ali-mi9up
    @Ali-mi9up 5 років тому +13

    Those saying that pseudocodes is wrong are incorrect, you can have another implementation that's right too

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

    Thank you so much for amazing videos! Great repetition before exams.

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

    All your sort videos were awesome!!! Thank you so much bc I needed it for my final tmw lol

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

    This channel is underrated man!

  • @VihaanKumar-o1s
    @VihaanKumar-o1s 10 місяців тому

    excellent explanation.........very simple and easy to understand

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

    Awesome simple and straight to the point - subscribed!

  • @UtshaKSaha
    @UtshaKSaha 5 місяців тому

    This was such a good video, some things just need visualizations.

  • @entruh4520
    @entruh4520 4 роки тому

    Thanks man. Wish me luck for my test tomorrow

  • @vendita2
    @vendita2 4 роки тому

    Thank u, didnt have time for big explanations

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

    Awesome videos, great refreshers before finals

  • @secretnobody6460
    @secretnobody6460 26 днів тому

    so much better than my 3 hour lectures

  • @player-vo8yb
    @player-vo8yb 3 роки тому +1

    i like these short vids
    to the point and no padding like those other shitty ass vids
    thank you kind sir

  • @kingcookie3920
    @kingcookie3920 5 років тому +87

    the pseudocode works but I would do "N-i" instead of "N-1"

    • @jesusbojorquez2252
      @jesusbojorquez2252 5 років тому +26

      N - i - 1

    • @kingcookie3920
      @kingcookie3920 5 років тому +8

      @@jesusbojorquez2252 why? i starts with 1.
      First round you need to compare 6 times. That is N-1
      Second you compare 5 times. That is N-2
      Overall you have to do this 6 times
      (Not an insult, I am probably just don't get it)

    • @jesusbojorquez2252
      @jesusbojorquez2252 5 років тому +16

      @@kingcookie3920 I wrote N - i - 1 because if you look at the second for it goes from 0 to N -1, so when j == N - 1 it will compare a[N - 1] with a[N] which doesn't exist in the array. "FIrst round you need to compare 6 times" yes that's right but remember that the second for start from 0 so you don't need to go to N-1 you just need to go to N - 2.

    • @renzserilo7142
      @renzserilo7142 4 роки тому +28

      Just an ordinary boy here just to read these comments, i’m not crazy to argue with a king specially with jesus.

    • @svenwillett1750
      @svenwillett1750 4 роки тому

      You're doing an advanced bubble pass, a normal one still does the rest even though you know the last is known.

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

    Hello, i just eant to thank you for you effort in this short videos I have adhd and I always find it so hard to understand CS concepts such as datastructers and long videos don't work for me at all also I am a visual learner so, I'm really thankful!

  • @CephasTheWanderer
    @CephasTheWanderer 5 років тому

    Awesome. Short, sweet, simple

  • @cravinghydra30
    @cravinghydra30 4 роки тому +1

    Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order and it arranges them in increasing order.#IISH 11CSHLIC

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

    I love having to put the video in 2x speed. This should be a 20 second video

  • @prashanthreddy1502
    @prashanthreddy1502 4 роки тому +8

    The bubble sort method is a nice and simple method of sorting an array. It however looks to be very time consuming and inefficient as it would have to run almost as many times as the size of the array and it would be highly inefficient with larger arrays. #IISH11CSHLIC

  • @random-0
    @random-0 3 роки тому

    Best video on bubble sort

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

    Finally understand it, thanks so much

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

    Thank you! This really was helpful.

  • @nirkiotaxy
    @nirkiotaxy 5 місяців тому

    ООООО, НАКОНЕЦ Я ПОНЯЛ ЭТОТ МОМЕНТ!!!
    То, что при первом прохождении цикла, большее число однозначно отсортируется и будет стоять в конце массива, из-за чего можно не проводить последующие проверки с ним

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

    Nice video! Helpped I lot to teach this

  • @sanadesai13
    @sanadesai13 4 роки тому +8

    @michael, Thanks for the video, but shouldn't the second FOR loop be "for j from 0 to N-2". i.e till the second last element.
    Because if it reaches "N-1" i.e the last element then j+1 = N which will give out of bound error.

    • @RAHULTMNT100
      @RAHULTMNT100 2 роки тому +2

      you are correct
      the values should be,
      i < nSize - 1 ( as i reaches nSize -1 , array will be sorted)
      j < nSize - i - 1 ( after i iterations, the last i elements are already sorted)

  • @이석원-x9p
    @이석원-x9p 5 років тому +1

    I like colors you made.

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

    this was very helpful, Thank you!

  • @Withmusclecutie
    @Withmusclecutie Місяць тому +1

    Wouldnt it be more efficient if it didnt look at the number combos that didnt change

  • @radhikasoni8105
    @radhikasoni8105 5 років тому +1

    Brilliant explanation!!
    Please make a video on Radix sort.

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

    Great video! Thank you so much.

  • @jamaicandjnado
    @jamaicandjnado 5 років тому +2

    Perfection. Thank you.

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

    Very helpful thanks for the video!

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

    This was great. Thank you!

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

    Thank you so much, very helpful!

  • @afaqa.s.9887
    @afaqa.s.9887 5 років тому +1

    Simply, awesome...

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

    great and efficient to learn like this

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

    Nice video!! It helped me a lot

  • @nty449
    @nty449 4 роки тому

    my teacher showed us this dumb ass dance thing and I was like WTF IS THIS GARBAGE. this is so much more helpful thank you. I actually get it now

  • @lukehatcher98
    @lukehatcher98 4 роки тому

    Just what I needed!

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

    This psuedocode works, but it's literally doing twice the number of iterations it needs to, and this is wasteful code. It should be:
    For i = N-1 to 0
    For j = 0 to i
    if a[j] < a[j+1]
    swap(a[j], a[j+1])
    If after the first pass of j, the highest value in the array will be sitting in N. Knowing this, the second pass of j should terminate one space earlier, which will happen when i decrements toward 0.

  • @riyamani8161
    @riyamani8161 4 роки тому

    #IISH 11CSHLIC The video helped me understand the process of bubble sorting and how the iteration works. It is a comparison of consecutive variables that gives the right form of number as the end result.

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

    Great explanation!

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

    So bubble sort is a bit like the opposite of selection sort, isn't it? Since with selection sort, you have a sorted partition on the left at any given iteration, and with bubble sort you have a sorted partition on the right at any given iteration.

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

    Nicely done!

  • @StewieGriffin
    @StewieGriffin 5 років тому

    Bubblesort is basically the MOST BASIC SORT EVER

  • @JuanArth378
    @JuanArth378 4 роки тому

    Thank you it was very helpful

  • @joebradley1239
    @joebradley1239 7 років тому +14

    Great video! im doing GCSE computer science and i was just wondering what would an exam style question look like? would it simply ask to sort these numbers using a bubble sort (show your working) or breifly explain what a bubble sort does? thankyou

    • @antoniojg-b8284
      @antoniojg-b8284 6 років тому +3

      In my experience, it's likely the first one.

    • @ameliewolfbane7733
      @ameliewolfbane7733 6 років тому +8

      One question might be what will the last two numbers be after the first iteration (the answer would be 1 , 9)

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

    humans hav voice for reason : nice work

  • @devops-k1p
    @devops-k1p Рік тому

    Currently done with 2h lecture, now time for 2h lab. Leave me alone bubble sort!

  • @HabirRibah
    @HabirRibah 5 років тому +1

    You're great. Thank you

  • @Tim1-z6b
    @Tim1-z6b Рік тому +1

    Bubble sort Javascript code:
    function bubbleSort(arr) {
    for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
    if (arr[j] > arr[j + 1]) {
    let temp = arr[j];
    arr[j] = arr[j + 1];
    arr[j + 1] = temp;
    }
    }
    }
    return arr;
    }

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

    very good, thank you❤️👍

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

    From @interviewkickstart I learned that,
    Its start comparison from right to left.
    So second for loop,
    For j from N to i
    Everything looks similar but here heavy element sorted at right. And Interviewkickstart video , in every steps lighter elements sorted at left

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

    I would say the pseudo code isnt optimal as you dont need to check the last element of each itteration again.
    i would change it to this
    For i from 1 to N
    For j from 0 to N - i // the 1 to i instead
    if a[ j ] > a[ j+1]
    swap ( a[ j ], a[ j + 1] )

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

    is it me or this is probably the slowest sorting algo?
    thanks for a more clear explanation

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

    Guys is N-1 not N-i just saying its the last value not the array size a[i]

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

    Small typo: I believe in the pseudocode, the j loop should be from 0 to N-i

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

    you saved me, thanks a lot man

  • @npkarki
    @npkarki 3 місяці тому

    Just seen this before 1 hour of exam 😂😂

  • @Kumar-zo9xe
    @Kumar-zo9xe 4 роки тому

    The numbers were exchanging from left to right by arrenging in order like 453621=123456

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

    Subscribed, thanks!

  • @yahavx
    @yahavx 5 років тому +1

    Should mentioned that the worst case of n^2 is also the best case. Great video though

    • @RahulSiyanwal
      @RahulSiyanwal 5 років тому +1

      best case is O(n)

    • @yahavx
      @yahavx 5 років тому

      @@RahulSiyanwal No because you iterate the array N times whatsoever. even if the array is already sorted. Maybe you're confusing with insertion sort

    • @shpootz
      @shpootz 5 років тому +1

      @@yahavx just update a flag hasSwaped and exit on false..
      would that give O(n) 4 best case?

    • @RahulSiyanwal
      @RahulSiyanwal 5 років тому

      @@yahavx That's my point. You only have to iterate an array N times and not n^2 times. You have mentioned that "worst case of n^2 is also the best case" which is not correct or I have misunderstood your statement.

    • @zapien120
      @zapien120 4 роки тому

      You are using nested for loops. Their for its O(n^2) best and worse

  • @tksnapz5003
    @tksnapz5003 6 років тому

    So helpful thank you!

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

    so, essentially, the worst case scenario is where it's already sorted but in reverse order, right?

  • @shafaf1
    @shafaf1 4 роки тому

    Simple and understandable

  • @David-ze7xb
    @David-ze7xb 5 років тому

    thanks for helping me pass an exam

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

    for an strange reason, running this pseudocode in python has a bug, I don't know why, but comparing 2 elements of the list, for example: 4>23, returns TRUE, because. it seems to be comparing only the first digit (4>2), I solved it by converting the current value and the next value to integer before comparing, does anyone know why this happens?

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

    I know how to do the problem but I don't know how to tell the computer how long I want this process to continue. Do you have any suggestion of books to read or watch?

  • @NyashaMandizvidza-vp3do
    @NyashaMandizvidza-vp3do Рік тому

    This is amazing