Heap sort in 4 minutes

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ส.ค. 2016
  • Step by step instructions showing how to run heap sort.
    Code: github.com/msambol/dsa/blob/m... (different than video, I added this retroactively)
    Source: ind.ntou.edu.tw/~litsnow/al98/...
    LinkedIn: / michael-sambol

ความคิดเห็น • 274

  • @cardakeys
    @cardakeys 4 ปีที่แล้ว +1643

    Everybody gangsta till pseudocode kicks in

    • @vaiebhavpatil2340
      @vaiebhavpatil2340 3 ปีที่แล้ว +20

      fr😂

    • @pedrovillar2746
      @pedrovillar2746 3 ปีที่แล้ว +3

      True lol

    • @miloradowicz
      @miloradowicz 2 ปีที่แล้ว +41

      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 2 ปีที่แล้ว +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 2 ปีที่แล้ว

      @Marley Alexzander Instablaster ;)

  • @timetraveler6780
    @timetraveler6780 5 ปีที่แล้ว +424

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

    • @fridaaa0
      @fridaaa0 2 ปีที่แล้ว +77

      *listens to heap sort as meditation*

    • @Chesspro-yb6hl
      @Chesspro-yb6hl ปีที่แล้ว +2

      Lol

  • @karatefrosch17
    @karatefrosch17 2 หลายเดือนก่อน +18

    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.

  • @ankitb3954
    @ankitb3954 6 ปีที่แล้ว +39

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

  • @Blueglitter73
    @Blueglitter73 6 ปีที่แล้ว +39

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

  • @joevtap
    @joevtap 2 ปีที่แล้ว +23

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

  • @parikshit804
    @parikshit804 6 ปีที่แล้ว +41

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

  • @devendratapdia11
    @devendratapdia11 4 ปีที่แล้ว +5

    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.

  • @helo-fn8pn
    @helo-fn8pn 7 ปีที่แล้ว +196

    ok, better than my lecture just read through slides

    • @anonymuzz5827
      @anonymuzz5827 5 ปีที่แล้ว +1

      i agree

    • @Jonasbagger
      @Jonasbagger 3 ปีที่แล้ว +7

      Until you are asked to prove the time complexity ^^

  • @BlerdGrid
    @BlerdGrid 5 ปีที่แล้ว +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!!

  • @weiliu_
    @weiliu_ 4 หลายเดือนก่อน +1

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

  • @ethanbai5712
    @ethanbai5712 6 ปีที่แล้ว +3

    Nice video, Michael. This is very clearly explained.

  • @RealSlimRoeder
    @RealSlimRoeder 4 ปีที่แล้ว +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

  • @daksneezian1252
    @daksneezian1252 6 ปีที่แล้ว +4

    Your videos are incredible, very useful - thanks!

  • @yuvaranikannan9297
    @yuvaranikannan9297 5 ปีที่แล้ว +5

    Short and sweet explanation. Thank you:)

  • @mdnamussakib8077
    @mdnamussakib8077 9 หลายเดือนก่อน +1

    Great explanation. Short and precious.

  • @mateopuna98
    @mateopuna98 3 ปีที่แล้ว +2

    Excellent explanation, simple and clear!

  • @vascanor6085
    @vascanor6085 3 ปีที่แล้ว

    after watching several videos, I find it the best

  • @elcar5468
    @elcar5468 2 หลายเดือนก่อน

    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.

  • @zhonq
    @zhonq 6 ปีที่แล้ว +4

    gonna share this channel w/ all my CS friends omg

    • @masterjif9506
      @masterjif9506 19 วันที่ผ่านมา

      Well congrats on y’all’s graduation

  • @attilatoth1396
    @attilatoth1396 6 ปีที่แล้ว +1

    Good as always! Thank you Michael!

  • @shacharh5470
    @shacharh5470 5 ปีที่แล้ว +83

    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 5 ปีที่แล้ว +10

      Me too..stupid video

    • @a_medley
      @a_medley 5 ปีที่แล้ว +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.

  • @LucasNaruto8107
    @LucasNaruto8107 6 ปีที่แล้ว

    Dude, your short explanation is awesome

  • @iovewhalien2191
    @iovewhalien2191 ปีที่แล้ว

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

  • @nebularzz
    @nebularzz ปีที่แล้ว

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

  • @Mahdi_757
    @Mahdi_757 ปีที่แล้ว

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

  • @remingtonward5356
    @remingtonward5356 ปีที่แล้ว

    This saved me for my algorithms test. Thank you

  • @ranjanaashish
    @ranjanaashish 6 ปีที่แล้ว

    greatly simplified. Superb explanation :)

  • @kogmawgaming
    @kogmawgaming 15 วันที่ผ่านมา

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

    • @MichaelSambol
      @MichaelSambol  15 วันที่ผ่านมา +1

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

  • @nosignal5908
    @nosignal5908 5 หลายเดือนก่อน

    Most amazing and simplified explanation

  • @franklounatics
    @franklounatics 4 ปีที่แล้ว +1

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

  • @Satoabi
    @Satoabi 4 ปีที่แล้ว +19

    I turned it into "heap sort in 2 minutes"

  • @jatinkumar4410
    @jatinkumar4410 4 ปีที่แล้ว +1

    Very clearly explained. Thank you sir.

  • @Nyck1236
    @Nyck1236 3 ปีที่แล้ว +1

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

  • @rolo_1
    @rolo_1 2 หลายเดือนก่อน

    best explanation on how it works

  • @Jkauppa
    @Jkauppa ปีที่แล้ว

    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

  • @raphaelpaillard557
    @raphaelpaillard557 2 ปีที่แล้ว +1

    Thanks for everything you're doing buddy

  • @alex19991014
    @alex19991014 2 ปีที่แล้ว +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?

  • @matts8791
    @matts8791 8 หลายเดือนก่อน

    Awesome video, way better than my professor!

  • @anonymous-dp7od
    @anonymous-dp7od 6 หลายเดือนก่อน

    Thank you sir. Just simple to understand ☺️

  • @nqobaninhlengethwa8363
    @nqobaninhlengethwa8363 5 ปีที่แล้ว

    The best tutorial. Thank you

  • @li-pingho1441
    @li-pingho1441 3 ปีที่แล้ว

    The best tutorial of heap sort :)

  • @tastelesstouch
    @tastelesstouch 7 ปีที่แล้ว

    Very nice explanation. Just subscribed.

  • @norbertkolud2878
    @norbertkolud2878 2 หลายเดือนก่อน

    saving lives to this day, thank you very much

  • @lounaemile7143
    @lounaemile7143 4 ปีที่แล้ว

    simple and effective, thanks for the video

  • @ozzy8489
    @ozzy8489 ปีที่แล้ว

    Very nice explanation!! Thanks a lot. 😊

  • @ogradus
    @ogradus 3 ปีที่แล้ว +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?

  • @elias-9395
    @elias-9395 5 ปีที่แล้ว +1

    Very informative. Thanks a lot

  • @ambalikasharma6474
    @ambalikasharma6474 3 ปีที่แล้ว

    Well explained sir, very nice explanation.

  • @vinothselvaraj8005
    @vinothselvaraj8005 5 ปีที่แล้ว

    Very good explanation. Thanks

  • @user-cr6zu5mm5j
    @user-cr6zu5mm5j ปีที่แล้ว

    so clearly explained! Wow thank you

  • @tamhuynh772
    @tamhuynh772 6 ปีที่แล้ว

    good job Michael!

  • @alanoudalmotairi4544
    @alanoudalmotairi4544 4 ปีที่แล้ว

    From 2020 that’s still bright video 👩‍💻

  • @kipchumba1276
    @kipchumba1276 2 ปีที่แล้ว

    Amazing and concise. Thanks

  • @ibadshaikh2215
    @ibadshaikh2215 4 ปีที่แล้ว

    Great Explanation..Keep it up buddy

  • @rredy
    @rredy 5 หลายเดือนก่อน

    Amazing explanation

  • @desheen5056
    @desheen5056 7 ปีที่แล้ว

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

  • @APC9906
    @APC9906 4 ปีที่แล้ว

    Videos are awesome man.

  • @amnishsingh9093
    @amnishsingh9093 10 หลายเดือนก่อน

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

  • @sivanisanku881
    @sivanisanku881 6 ปีที่แล้ว

    Tq sir less time more information about heap sort

  • @foobars3816
    @foobars3816 3 ปีที่แล้ว +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.

  • @tobyto4614
    @tobyto4614 2 ปีที่แล้ว +2

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

  • @NickWinters
    @NickWinters 15 วันที่ผ่านมา +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  14 วันที่ผ่านมา

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

  • @tomaskunickij6912
    @tomaskunickij6912 6 ปีที่แล้ว

    Great explanation! :)

  • @jose-lael
    @jose-lael 7 ปีที่แล้ว

    Your videos are great :)

  • @madhavanand756
    @madhavanand756 3 ปีที่แล้ว +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 3 ปีที่แล้ว

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

  • @gibi1313
    @gibi1313 ปีที่แล้ว +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 11 หลายเดือนก่อน

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

    • @seanharris5594
      @seanharris5594 10 หลายเดือนก่อน

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

  • @nextadastraschool7230
    @nextadastraschool7230 4 หลายเดือนก่อน

    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 ❤

  • @amoghasoda
    @amoghasoda 6 ปีที่แล้ว

    Mindblowing!

  • @sulee1058
    @sulee1058 4 ปีที่แล้ว

    love your explanation thx

  • @GermanCoDClan
    @GermanCoDClan หลายเดือนก่อน

    awesome, very much appreciated!

  • @Cos_Wayne
    @Cos_Wayne ปีที่แล้ว

    Clear explanation. Thanks.

  • @MattaparthiShivaBhargav
    @MattaparthiShivaBhargav 3 ปีที่แล้ว

    This is the Ffff reason I subscribed

  • @OK-cv1pt
    @OK-cv1pt 2 ปีที่แล้ว

    I have mid term comming up, thanks sir

  • @davidjames1684
    @davidjames1684 4 ปีที่แล้ว +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.

  • @shaund34
    @shaund34 4 ปีที่แล้ว +2

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

  • @sohamdave1192
    @sohamdave1192 4 ปีที่แล้ว

    Thanks a lot, Love from INDIA

    • @jandemel5246
      @jandemel5246 3 ปีที่แล้ว

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

  • @jroseme
    @jroseme ปีที่แล้ว +1

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

  • @SCGamingVN
    @SCGamingVN 5 ปีที่แล้ว +2

    thank you very helpful

  • @ashw6015
    @ashw6015 6 ปีที่แล้ว

    Beautiful.

  • @fhaseando
    @fhaseando 5 ปีที่แล้ว

    Very helpful!!
    But I think that there is an error in the pseudocode at Heapify definition in the second if :
    I think that you need another condition: A[right]>A[left]

    • @Uthalerebaba-nr4qi
      @Uthalerebaba-nr4qi ปีที่แล้ว +1

      No you don't need that condition. Remember, this isn't a binary search tree. We just need to ensure that the parent element is greater than their children.

  • @salsamancer
    @salsamancer 6 หลายเดือนก่อน

    Always appreciate CS tutorials without the Hindi accent 👍🏿

  • @euijinjung7619
    @euijinjung7619 7 ปีที่แล้ว +1

    Thank you!

  • @samplower5860
    @samplower5860 4 ปีที่แล้ว

    how did you figure out the big O times?

  • @DarkGT
    @DarkGT 7 ปีที่แล้ว +91

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

    • @boggeshzahim3713
      @boggeshzahim3713 5 ปีที่แล้ว +86

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

    • @TheAmayzinRayzin
      @TheAmayzinRayzin 5 ปีที่แล้ว +5

      @@boggeshzahim3713 I said the same thing lol

    • @ishaanj8023
      @ishaanj8023 4 ปีที่แล้ว +8

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

  • @piltonswrangbrahma5140
    @piltonswrangbrahma5140 ปีที่แล้ว

    Noice...much better video than others

  • @coroutinedispatcher
    @coroutinedispatcher 4 ปีที่แล้ว +1

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

  • @emilsonksen9399
    @emilsonksen9399 6 ปีที่แล้ว +1

    What does "floor" in the psuedocode mean?

    • @AhmadTalkss
      @AhmadTalkss 6 ปีที่แล้ว +4

      means to round down. Ex: 3.5 floor = 3

  • @maya.n
    @maya.n 5 ปีที่แล้ว +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 5 ปีที่แล้ว +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 5 ปีที่แล้ว +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 ปีที่แล้ว

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

  • @kewei4767
    @kewei4767 ปีที่แล้ว

    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  ปีที่แล้ว +1

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

  • @sinto4105
    @sinto4105 4 ปีที่แล้ว +7

    at 1:35 why you are sorting the sorted array

    • @alexanderphilipsieber8251
      @alexanderphilipsieber8251 4 ปีที่แล้ว +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

  • @ritabritabasak5652
    @ritabritabasak5652 3 ปีที่แล้ว

    Really good, thanks a lot

  • @EricSartor
    @EricSartor 6 ปีที่แล้ว +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 5 ปีที่แล้ว +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 ปีที่แล้ว

      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 ปีที่แล้ว

      @@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?

  • @AkiZukiLenn
    @AkiZukiLenn 3 ปีที่แล้ว

    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 ปีที่แล้ว

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

  • @the_smell_of_coffee
    @the_smell_of_coffee ปีที่แล้ว +1

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

  • @AmNotLegend
    @AmNotLegend ปีที่แล้ว

    heap sort for reversing a sorted list?

  • @mdjcmdjc5842
    @mdjcmdjc5842 5 ปีที่แล้ว

    does anyone have a ruby implementation of this pesudo code by any chance ?

  • @TheGameprogirl
    @TheGameprogirl 4 ปีที่แล้ว

    Why do you perform heapify on the left subtree or sometimes the right subtree?

    • @shazzl
      @shazzl 2 ปีที่แล้ว

      according to max heap property parent node will always be greater than the child nodes and it is applicable for the subtrees as well.

  • @arafat6578
    @arafat6578 5 ปีที่แล้ว

    So helpful video sir