Heap sort in 4 minutes

Sdílet
Vložit
  • čas přidán 13. 09. 2024
  • Step by step instructions showing how to run heap sort.
    Code: github.com/msa... (different than video, I added this retroactively)
    Source: ind.ntou.edu.tw...
    LinkedIn: / michael-sambol

Komentáře • 282

  • @cardakeys
    @cardakeys Před 4 lety +1875

    Everybody gangsta till pseudocode kicks in

    • @vaiebhavpatil2340
      @vaiebhavpatil2340 Před 3 lety +22

      fr😂

    • @pedrovillar2746
      @pedrovillar2746 Před 3 lety +3

      True lol

    • @miloradowicz
      @miloradowicz Před 3 lety +49

      Wriwitng pseudo code is easy once you understood the algorithm. Reading someone's pseudo code on the other hand can be quite annoying, because they may use different naming conventions, different syntax and even different "language" than you.

    • @marleyalexzander9326
      @marleyalexzander9326 Před 3 lety +3

      I guess Im randomly asking but does someone know of a method to get back into an Instagram account..?
      I somehow lost my password. I appreciate any tips you can give me!

    • @isaiahmarcel7844
      @isaiahmarcel7844 Před 3 lety

      @Marley Alexzander Instablaster ;)

  • @timetraveler6780
    @timetraveler6780 Před 5 lety +456

    Audio is so clear, amazingly static free( it is so near to being a meditation app audio).

  • @karatefrosch17
    @karatefrosch17 Před 5 měsíci +41

    I am genuinely impressed at how shit my professor is he takes 5 slides to explain this and never asks any of the many questions i had. This video did more for me than 2 lectures.

    • @Raccoon_TheGreat
      @Raccoon_TheGreat Před měsícem

      my lecture explained it over 7 slides, kind of understandable, but the video sure helped

  • @ankitb3954
    @ankitb3954 Před 6 lety +45

    you just saved my semester. I have been binging your channel

  • @Blueglitter73
    @Blueglitter73 Před 6 lety +40

    This was very clear to me. Now I understand heap sort in a visual sense thank you

  • @devendratapdia11
    @devendratapdia11 Před 5 lety +6

    First i felt why u didnt explain how to build max heap but then it becomes clear as thats not the point of video and can be learnt seperately. You are making these look so simple. Thumbs up to you.

  • @parikshit804
    @parikshit804 Před 6 lety +42

    I like the small time of these videos. Neat graphics. Good work.

  • @helo-fn8pn
    @helo-fn8pn Před 7 lety +203

    ok, better than my lecture just read through slides

  • @joevtap
    @joevtap Před 2 lety +24

    What a nice explanation! That made my understanding of the algorithm a lot easier, thanks a lot

  • @weiliu_
    @weiliu_ Před 8 měsíci +2

    What a clear explanation. I couldn't understand what teacher said till I see this video 👍

  • @BlerdGrid
    @BlerdGrid Před 6 lety +2

    The best part is the very last portion where everything is summed up and pseudocode is given. Thanks! Exploring more videos to prepare for an interview!!

  • @tobyto4614
    @tobyto4614 Před 2 lety +8

    Doesn't make sense, if the build max heap has already sorted in desc, why would I need to do another sort again.

    • @luisf_lima
      @luisf_lima Před 5 dny

      that's what I thought, it doesn't make any sense

  • @INT_MAX
    @INT_MAX Před 7 lety +202

    Thank you for speaking proper English.

    • @Sitback
      @Sitback Před 6 lety +3

      LMFAOOOOOO [TRUUUUUUUUUUUUU]

    • @Rajonty
      @Rajonty Před 6 lety

      LOL

    • @dreamy6517
      @dreamy6517 Před 5 lety +3

      Thats fucking racist tho

    • @raphaelandrade555
      @raphaelandrade555 Před 5 lety +5

      @@dreamy6517 That's not racist, there are people who speak bad english, what's the problem with that?

    • @randomhappyguy6719
      @randomhappyguy6719 Před 5 lety +6

      @@raphaelandrade555 Consider this: someone's native language is English and has lived in Japan for 2 years, and this person makes a video in Japanese, which incurs some native Japanese speaker saying "he's not saying proper/bad Japanese". It's like laughing at a 1-month-old not being able to walk.

  • @kogmawgaming
    @kogmawgaming Před 4 měsíci

    I owe my entire CS degree to videos like this. Explains stuff much better than professors you have to pay for : ^)

    • @MichaelSambol
      @MichaelSambol  Před 4 měsíci +3

      when you IPO your tech company you can toss me 1% ;)

  • @shacharh5470
    @shacharh5470 Před 5 lety +84

    You skipped all the details on how build-max-heap works! That was the part I wanted to see, I need to understand how it manages complexity of O(n)

    • @navikh333
      @navikh333 Před 5 lety +10

      Me too..stupid video

    • @a_medley
      @a_medley Před 5 lety +20

      it manages O(n) by using a bottom-up approach. Each sub-tree in a heap must also maintain the heap property. When you run build-max-heap the runtime depends on the height of the tree. Since you can safely build a heap bottom-up, you would get something like this: O(n + (1/2)n + (1/4)n + (1/8)n + (1/16)n + ...), which simplifies to O(2n), or just O(n). That's not exactly what happens, but it's along those lines. You should google "linear time build heap" for more info.

  • @elcar5468
    @elcar5468 Před 5 měsíci

    This process was very confusing in my lecture but looking at it with the array and the tree side-by-side made it crystal clear what was going on.

  • @Mahdi_757
    @Mahdi_757 Před rokem

    Great tomorrow our mem will be taken our class test..now i watching your video😊great.. ❤

  • @mdnamussakib8077
    @mdnamussakib8077 Před rokem +1

    Great explanation. Short and precious.

  • @RealSlimRoeder
    @RealSlimRoeder Před 4 lety +4

    i know this is a little late but thank you so much for this video!!
    the way you explained it, i understood this much better than when my prof talked about it in class.
    bonus points for the pseudocode at the end :D

  • @vascanor6085
    @vascanor6085 Před 3 lety

    after watching several videos, I find it the best

  • @yuvaranikannan9297
    @yuvaranikannan9297 Před 5 lety +5

    Short and sweet explanation. Thank you:)

  • @iovewhalien2191
    @iovewhalien2191 Před rokem

    omg my textbook made this so confusing, but this gave me so much clarity thank you so much!

  • @ethanbai5712
    @ethanbai5712 Před 6 lety +3

    Nice video, Michael. This is very clearly explained.

  • @zhonq
    @zhonq Před 6 lety +4

    gonna share this channel w/ all my CS friends omg

  • @Satoabi
    @Satoabi Před 5 lety +19

    I turned it into "heap sort in 2 minutes"

  • @raphaelpaillard557
    @raphaelpaillard557 Před 2 lety +1

    Thanks for everything you're doing buddy

  • @remingtonward5356
    @remingtonward5356 Před rokem

    This saved me for my algorithms test. Thank you

  • @nosignal5908
    @nosignal5908 Před 9 měsíci

    Most amazing and simplified explanation

  • @daksneezian1252
    @daksneezian1252 Před 6 lety +4

    Your videos are incredible, very useful - thanks!

  • @foobars3816
    @foobars3816 Před 4 lety +6

    1:38 and we are done, just call reverse on the array.... wait why are you messing it up again? I think the example data made this more confusing as it ended up sorted after the first build-max-heap call.

  • @rolo_1
    @rolo_1 Před 5 měsíci

    best explanation on how it works

  • @mateopuna98
    @mateopuna98 Před 3 lety +2

    Excellent explanation, simple and clear!

  • @salsamancer
    @salsamancer Před 10 měsíci

    Always appreciate CS tutorials without the Hindi accent 👍🏿

  • @Nyckoka
    @Nyckoka Před 3 lety +1

    Yes, yes, yes. This is insanely good. Thanks for the video

  • @norbertkolud2878
    @norbertkolud2878 Před 6 měsíci

    saving lives to this day, thank you very much

  • @gibi1313
    @gibi1313 Před rokem +3

    I think the array you used in this example might be misleading, as when you do the max-heap you obtain a descendent sorted array and from there you can just flip it to get the sorted array.

    • @AahanaHegde-py7ng
      @AahanaHegde-py7ng Před rokem

      yes this is what is confusing me, im new to programming, but can you explain why just using max heap isnt enough?

    • @seanharris5594
      @seanharris5594 Před rokem

      @@AahanaHegde-py7ng max-heap just ensures that the parent node is greater than its children which doesn't guarantee that the tree written in the array form will be sorted in descending order. For example, when the original array has max-heap applied to it in the video it returns the array 9 8 5 3 2 1 (which is sorted descending). However, if you look at the representation of the tree in the video and imagine flipping the left sub tree with the right you would get the array 9 5 8 1 3 2. This still satisfies the condition that the parent node is greater than the child, but the array is no longer sorted.

  • @LucasNaruto8107
    @LucasNaruto8107 Před 6 lety

    Dude, your short explanation is awesome

  • @DarkGT
    @DarkGT Před 7 lety +92

    after 5 videos explaining Heap Sort I get it now, next binary tree...God help me.

    • @boggeshzahim3713
      @boggeshzahim3713 Před 5 lety +87

      Kind of weird to understand a heap sort without understand what a binary tree is

    • @TheAmayzinRayzin
      @TheAmayzinRayzin Před 5 lety +5

      @@boggeshzahim3713 I said the same thing lol

    • @ishaanj8023
      @ishaanj8023 Před 4 lety +8

      A binary tree is just a tree in which each node has 2 children at most, just a definition.

  • @ogradus
    @ogradus Před 3 lety +2

    I don't get it, build max heap sorts the tree, right?
    Why not just build the array with the sorted tree backwards, linearly?

  • @alanoudalmotairi4544
    @alanoudalmotairi4544 Před 4 lety

    From 2020 that’s still bright video 👩‍💻

  • @TheRealKitWalker
    @TheRealKitWalker Před 2 lety +2

    Heap sort in 4 mins!? 😱 You desperately need code optimization 😂 just kidding. Great video, well well explained. thanks ✌️👏

  • @mattm7831
    @mattm7831 Před 3 lety

    My professor didn't even post a class, she just uploaded links to all your sort videos instead.

  • @the_smell_of_coffee
    @the_smell_of_coffee Před rokem +1

    Not all heroes wear capes. You saved me😮‍💨

  • @matts8791
    @matts8791 Před rokem

    Awesome video, way better than my professor!

  • @sinto4105
    @sinto4105 Před 4 lety +7

    at 1:35 why you are sorting the sorted array

    • @alexanderphilipsieber8251
      @alexanderphilipsieber8251 Před 4 lety +1

      Lokesh Joshi it was a coincidence that his max heap was already sorted in descending order. That will not always be the case. Should be a different example to avoid confusion like this

  • @nebularzz
    @nebularzz Před rokem

    great tutorial! took me a while to understand but now i get it thank you for teaching me this

  • @jatinkumar4410
    @jatinkumar4410 Před 4 lety +1

    Very clearly explained. Thank you sir.

  • @attilatoth1396
    @attilatoth1396 Před 7 lety +1

    Good as always! Thank you Michael!

  • @Jkauppa
    @Jkauppa Před 2 lety

    hash sort by distribution function (for example linear) direct bin division placement, subsort each bin, O(n), divide and conquer, extended quick sort by multiple pivots at once, the bins

  • @NickWinters
    @NickWinters Před 4 měsíci +1

    I'm confused. Why do you need two functions for this? In both cases the input was a tree and the output was a max heap.
    Maybe it's because we know that after the first one it's only the root node that is out of place, and we use another function that doesn't do unnecessary calculations related to other nodes. Then it'd be better if the name was more descriptive. Considering how it doesn't operate on the whole tree, it can be named something like heapify_branch instead

    • @MichaelSambol
      @MichaelSambol  Před 4 měsíci

      Could you take a look at the full playlist, I think it will help: czcams.com/play/PL9xmBV_5YoZNsyqgPW-DNwUeT8F8uhWc6.html.
      Code: github.com/msambol/dsa/blob/master/sort/heap_sort.py

  • @anonymous-dp7od
    @anonymous-dp7od Před 9 měsíci

    Thank you sir. Just simple to understand ☺️

  • @li-pingho1441
    @li-pingho1441 Před 3 lety

    The best tutorial of heap sort :)

  • @madhavanand756
    @madhavanand756 Před 3 lety +6

    1:35 At first function call of "Build Max Heap" we have sorted array in descending order, we fix this simply by swapping. Why are we performing these many operations ?
    Anyone please !

    • @surajmodi49
      @surajmodi49 Před 3 lety +1

      that just happens for this example.. doesn't happen always

  • @EricSartor
    @EricSartor Před 6 lety +3

    I'm very confused about something. When you call build-map-heap, you are sorting the entire array in descending order. At that point, the array is sorted. Why would you keep sorting after that point?

    • @temirlansafargaliyev8873
      @temirlansafargaliyev8873 Před 6 lety +1

      for example, left child can be less than right one, so now array is unsorted
      UPD: the left child of right child can be greater than the right child of left child. This try must be correct :)

    • @azzam7239
      @azzam7239 Před 2 lety

      Yes it's sorted but remember that it is *heap-ordered* and the resulting array is in the binary tree notation. The last portion of the algorithm is still necessary to have a sorted array by making use of the heap ordered array by continuously removing the largest element and reheapfying until you get your sorted array

    • @2004seraph
      @2004seraph Před rokem

      @@azzam7239 I don't really understand, in all the examples I see, the heap is physically stored as an array, and max heapify sorts that array perfectly, so why don't we just use that array?

  • @shaund34
    @shaund34 Před 4 lety +2

    After you buildMaxHeap the array was sorted already in inverse way. Why don't you just reverse it?

  • @franklounatics
    @franklounatics Před 5 lety +1

    Thank you for this sample of heap sort!!! 😍

  • @ranjanaashish
    @ranjanaashish Před 6 lety

    greatly simplified. Superb explanation :)

  • @jroseme
    @jroseme Před rokem +1

    Hmm, not sure I'm going to retain this long enough to get these test questions right lol

  • @gaganupadhyay2175
    @gaganupadhyay2175 Před 3 lety

    A very nice explanation. Thanks

  • @OK-cv1pt
    @OK-cv1pt Před 2 lety

    I have mid term comming up, thanks sir

  • @amnishsingh9093
    @amnishsingh9093 Před rokem +1

    Just one confusion, are those formulas in pseudocode correct?
    I thought they were
    left = 2i+1
    right=2i+2

  • @ozzy8489
    @ozzy8489 Před rokem

    Very nice explanation!! Thanks a lot. 😊

  • @sivanisanku881
    @sivanisanku881 Před 6 lety

    Tq sir less time more information about heap sort

  • @ambalikasharma6474
    @ambalikasharma6474 Před 3 lety

    Well explained sir, very nice explanation.

  • @elias-9395
    @elias-9395 Před 5 lety +1

    Very informative. Thanks a lot

  • @에헤헿-l7v
    @에헤헿-l7v Před rokem

    so clearly explained! Wow thank you

  • @aleksystrzecki205
    @aleksystrzecki205 Před rokem

    Awesome explanation

  • @GermanCoDClan
    @GermanCoDClan Před 5 měsíci

    awesome, very much appreciated!

  • @nextadastraschool7230
    @nextadastraschool7230 Před 8 měsíci

    Brother please make a video on threaded binary tree insertion, your videos are great and in less time, great for revisions and understanding complex concepts easily and quickly ❤

  • @nqobaninhlengethwa8363

    The best tutorial. Thank you

  • @piltonswrangbrahma5140

    Noice...much better video than others

  • @lounaemile7143
    @lounaemile7143 Před 4 lety

    simple and effective, thanks for the video

  • @ibadshaikh2215
    @ibadshaikh2215 Před 4 lety

    Great Explanation..Keep it up buddy

  • @vinothselvaraj8005
    @vinothselvaraj8005 Před 6 lety

    Very good explanation. Thanks

  • @alex19991014
    @alex19991014 Před 2 lety +1

    From the Pseudo code, as the for loop in the HeapSort() is already decrementing from n to 1, the (n - 1) in the HeapSort() should be useless?

  • @dominiorrr6510
    @dominiorrr6510 Před 6 měsíci

    What's the point of heap-sorting when we already have a sorted max-heap at 1:40? Yes, it's sorted backwards, but heapsorting takes more time than reversing a sorted array. I don't really get it.
    EDIT: I got it and I think the example used in the video wasn't that good, because a max-heap doesn't necessarily end up being a sorted array.

  • @MattaparthiShivaBhargav

    This is the Ffff reason I subscribed

  • @ojazzista
    @ojazzista Před 2 lety

    Is it right to assume that a Heap is ordered? We know that the heap property guarantees the greater or smaller relation between parent and children, but the overall tree may not be exactly ordered.

    • @MichaelSambol
      @MichaelSambol  Před 2 lety

      Can you take a look at the Heaps playlist? It provides more context.
      czcams.com/play/PL9xmBV_5YoZNsyqgPW-DNwUeT8F8uhWc6.html

  • @kipchumba1276
    @kipchumba1276 Před 2 lety

    Amazing and concise. Thanks

  • @Cos_Wayne
    @Cos_Wayne Před rokem

    Clear explanation. Thanks.

  • @tastelesstouch
    @tastelesstouch Před 7 lety

    Very nice explanation. Just subscribed.

  • @maya.n
    @maya.n Před 5 lety +3

    So, when you do max heap the first time, you get a sorted array but in a different direction. Can't you just make a new array that has the same values, but reversed and you are finished with the sorting?

    • @kylestormeyes4976
      @kylestormeyes4976 Před 5 lety +1

      yahh like put everything into a stack and then take it out in order ?? I thought the same :P hopefully someone will answer

    • @maya.n
      @maya.n Před 5 lety +1

      @@kylestormeyes4976 Actually, It is not correct. It only happens in certain cases, and this one is one of them. I should've deleted the comment when I realised that.

    • @Kokurorokuko
      @Kokurorokuko Před rokem

      @@maya.n Thank you for not deleting the comment. I thought the same thing.

  • @sohamdave1192
    @sohamdave1192 Před 4 lety

    Thanks a lot, Love from INDIA

    • @jandemel5246
      @jandemel5246 Před 3 lety

      Just having a chicken curry! Sending love to INDIA 🇮🇳 and all the boys who helped me with my university degree 😆

  • @SCGamingVN
    @SCGamingVN Před 5 lety +2

    thank you very helpful

  • @davidjames1684
    @davidjames1684 Před 4 lety +8

    I wonder if on average, plucking the 2 largest elements from the maxheap (if available), will speed things up. It seems wasteful to not pluck 2 at a time since we know the 2nd one will be one of the roots children. I traced this example on paper and it seems to work well. However I would like to know on say a 1 million entry tree (19 levels deep), how much faster it would be in real time. I would want to randomly generate 1 million numbers, store them for input to both "flavors" of heapsort, then make a table of outcomes. For example, 4.7 seconds classic heapsort, 4.6 seconds modified heapsort. I would also want to try cases with 1 million unique numbers, cases with 1 million small numbers with lots of repeats (like in the range 1 to 1000), and 1 million numbers with large gaps (like use 1 million random 32 bit numbers). I think the results would be interesting. Maybe someone can try it and report back.

  • @AyyyyyyyyyLmao
    @AyyyyyyyyyLmao Před rokem

    Amazing quality. Dark mode option perhaps?

    • @MichaelSambol
      @MichaelSambol  Před rokem

      thank you! all new videos for sure. I wish there was an easy option for dark mode on old videos, but it'd require remaking them.

  • @tamhuynh772
    @tamhuynh772 Před 6 lety

    good job Michael!

  • @JossinJax
    @JossinJax Před 4 lety +2

    In the example, I failed to see the difference in functionality between heapify and max heap :/ Good vid though, keep it up!

    • @ososzaid3444
      @ososzaid3444 Před 4 lety +2

      Think of heapify as a way to restore the max heap property "Max element at root" when we remove the root

  • @APC9906
    @APC9906 Před 5 lety

    Videos are awesome man.

  • @coroutinedispatcher
    @coroutinedispatcher Před 5 lety +1

    You haven't pass the n as a parameter in the heapify method.

  • @kewei4767
    @kewei4767 Před rokem

    Trying to understand build-max-heap and heapify here. Based on your explanation, build-max-heap arranges the tree so that the highest value goes to the top, forming max heap, then you swap it with the last element (bottom right) in the tree and remove it from the tree. Then you call heapify to gradually move the last element situated on top down for the child nodes (having higher value) to go up. How does the node choose which immediate child node to promote? And if what heapify does reiteratively is to eventually move the highest value node up to the top, why cant we call build-max-heap in the beginning?

    • @MichaelSambol
      @MichaelSambol  Před rokem +1

      Have you taken a look at the other two videos in this playlist? I think they'll help. czcams.com/play/PL9xmBV_5YoZNsyqgPW-DNwUeT8F8uhWc6.html

  • @generalginger7804
    @generalginger7804 Před rokem

    If we can build the maxheap why do we even need to sort it? Isnt it already sorted?

  • @Akshay-cj3hq
    @Akshay-cj3hq Před 4 lety +1

    Once we have the Max heap, isn’t it already sorted?? Now just reverse the order? What am I missing here?

    • @Tamam_Shud
      @Tamam_Shud Před 4 lety +1

      There is no guarantee that the array is sorted just because it is a Max Heap. Just look at the Max Heap at 2:20
      for instance.

  • @sulee1058
    @sulee1058 Před 4 lety

    love your explanation thx

  • @AkiZukiLenn
    @AkiZukiLenn Před 3 lety

    When you learnt Binary tree search and now eager to use it in this heap sort knowing it's not gonna be that smooth, bruh.

  • @kewei4767
    @kewei4767 Před rokem

    @1:38 isnt build-max-heap essentially 2 steps of heapify, first to swap between 2 and 8, then 2 and 9?

  • @desheen5056
    @desheen5056 Před 7 lety

    thank so much , liked and subscribed i have test and this helped me a lot ^^

  • @baljorsingh8935
    @baljorsingh8935 Před 3 lety

    Time complexity of BuildMaxHeap is O(nlogn)

  • @kafychannel
    @kafychannel Před rokem +1

    thank you

  • @ryklin1
    @ryklin1 Před 2 lety

    you omitted the initialization of the variable 'n' in the function heapify. n = elements_in(A)... it is the number of elements in the array of data.