Quicksort: Partitioning an array

Sdílet
Vložit
  • čas přidán 15. 10. 2014
  • This video shows how partitioning may be achieved, as part of the process of Quicksort. At the end of the partitioning process, the array is divided into two sub-arrays. One sub-array contains elements that are less than a chosen pivot, while the other contains elements that are more than the pivot. The partition function should return the position of the pivot.
    Note:
    While recording the video, I was focusing on moving the pivot to the (i+1)th position towards the end of the video, and in my hurry to finish the video, I had forgotten that a swap is sufficient. Agree (and this has been brought up many times in the past) that a swap at the last step is quicker and more efficient. However, it is not incorrect to shift - just inefficient. Actually, as mentioned right at the beginning, the objective here is to find the position of the pivot for the partition.

Komentáře • 1,3K

  • @whoopdeedoodude
    @whoopdeedoodude Před 5 lety +136

    All the fancy power points and drawing tablets and still the best explanation is a homie with paper cups.
    Thank you, sir!

  • @diegod.8518
    @diegod.8518 Před 3 lety +192

    I think it's funny how you don't use anything fancy to explain this, and yet your explanation is still one of the best.

    • @mtophir
      @mtophir  Před 3 lety +23

      Thank you. Glad you liked it :)

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

      Not sure.. have you seen the Hungarian dancers?

    • @hernancedillo6144
      @hernancedillo6144 Před 2 lety

      Thank you for the thorough explanation!!

    • @bipdopbop1878
      @bipdopbop1878 Před 2 lety

      Its called being a good teacher

  • @conea6891
    @conea6891 Před 3 lety +27

    This is the best video on Quicksort.
    However, at 4:05, we could just swap "Pivit" and the "i+1".

    • @crazguykwan8955
      @crazguykwan8955 Před 3 lety

      Yo I'm glad you said that. I was about to make a loop to shift them all (which would increase the time complexity). But your suggestion gives the same result (numbers to the right are larger than pivot) at a shorter time complexity

  • @srishtiavalakki751
    @srishtiavalakki751 Před 3 lety +42

    One of the most understanding videos i have seen so far about QuckSort. Thank you sir, extremely understanding

  • @justdanaus
    @justdanaus Před 3 lety +14

    I watched your instruction twice, not because it's hard to understand but so enjoyable to watch. Thank you

  • @fodecissokho9918
    @fodecissokho9918 Před rokem +13

    Usually I don't comment on video but I have to make an exception for this one because EVERYTHING WAS JUST PERFECTLY EXPLAINED....
    It changes from the videos where the guy takes more than 10mn to explain it in a way that will confuse more
    KEEP IT UPP 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥

    • @mtophir
      @mtophir  Před rokem +2

      Thank you for the compliment. Glad you like the video.

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

    This Legend is replying to comments even after 6 years.❤

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

      I'm no "legend" - I'm just an ordinary academic at a university. Thanks for your message, though.

  • @21void5
    @21void5 Před 4 lety +22

    this explains a lot why "i" started at index -1. Thanks.

  • @peterbalogh5622
    @peterbalogh5622 Před 2 lety +27

    This is a clear explanation, but I'd avoid shifting the array at 4:07 and just swap the 4 and the 8 (that is, the element at the pivot position with the element at i+1) since it's much less work and it will also result in guaranteeing what we need: everything to the left of i + 1 is smaller than the pivot, everything to the right of i+1 is greater than the pivot, and the pivot is at i.

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

      I thought the same, was surprised when he moved all the cups lol. Great video though! Super clear

    • @mtophir
      @mtophir  Před 2 lety

      Thanks both. This point has been discussed at length in the past. You may wish to read the video description. Thanks for watching!

  • @namanaggarwal5417
    @namanaggarwal5417 Před rokem +8

    not gonna lie, its the best video available for quicksort.

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

      Oh wow, that's the best compliment! Thank you for watching.

  • @bogslut
    @bogslut Před 5 lety +24

    This is the best explanation of quicksort I've seen on youtube and using simple paper cups of all things.

  • @shikha125
    @shikha125 Před rokem +30

    I've never seen such an amazing teaching method, thank you! This is gold

    • @mtophir
      @mtophir  Před rokem +7

      Thank you for the compliment!

  • @qaipak1
    @qaipak1 Před 7 lety +7

    Teacher took 40 mins. Book took 1 hour. This guy taught me in 10 mins.

  • @mapmappampam8758
    @mapmappampam8758 Před 7 lety +35

    you are very good at explaining this complex algorithm!
    with your calmness it was very easy to follow you step by step.
    you are the only one that actually helped me to understand :)
    Great job! Thank you!

    • @washboy
      @washboy Před 6 lety

      This should be the 'go to' video for quicksort explanation

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

    possibly best video on CZcams to understand Quicksort.
    Everyone just dry runs the algorithm - without clearly showing what's being done and why.
    You won 'em all!

    • @mtophir
      @mtophir  Před 2 lety

      Thanks~ I'm glad it helped.

  • @angelamunyao7877
    @angelamunyao7877 Před 5 lety +29

    This is the best explanation i've come across!

  • @estefaniac8260
    @estefaniac8260 Před 4 lety +59

    Amazing explanation! Thank you, Thank you, Thank you :) Very clearly explained and the visual aids were amazingly helpful.

    • @mtophir
      @mtophir  Před 4 lety +10

      You're welcome. Glad the video is of use to you.

  • @prashantgupta808
    @prashantgupta808 Před 3 lety +13

    probably the best explanation of quick sort in so less time,one thing to say to understand this better you first go throuhh the video explain by mycodeschool channel and then watch this and you will see clearly how things are working here.
    Hats off you sir!
    Great job.....

    • @mtophir
      @mtophir  Před 3 lety

      Thanks for your kind words

  • @zoriiginalx7544
    @zoriiginalx7544 Před rokem +10

    This is amazing! Great explanation. I could not understand quicksort until you masterfully and elegantly demonstrated how the algorithm works.

    • @mtophir
      @mtophir  Před rokem +1

      Thanks for the very kind words. Happy to help!

  • @adilbaig7022
    @adilbaig7022 Před 3 lety +4

    Only after watching this video did I understand that the `i` only increments when a swap happens, in effect keeping track of where the pivot should finally rest. The best explanation I've seen on quicksort partitioning!

  • @karimp59
    @karimp59 Před 2 lety +12

    Thank you prof KC Ang. After so many days of confusion I finally got clear explanation from your video. Thank you for virtually sharing your knowledges!!

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

      Glad it was helpful!

  • @samuelvalentine7846
    @samuelvalentine7846 Před rokem +6

    Please, can someone give all the humans, animals and plants as subscribers to this legend

    • @mtophir
      @mtophir  Před 10 měsíci +1

      Very flattered by your comment! Thanks for watching.

  • @nightfox6738
    @nightfox6738 Před rokem +4

    I find it really interesting how Quicksort as a sorting algorithm itself is intuitively quite simple but I've seen so many different methods for partitioning the array.

  • @katrinejensrud537
    @katrinejensrud537 Před 3 lety +10

    this was the best explanation of Quicksort in youtube

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

      Thank you. Glad you liked it.

  • @ibotah
    @ibotah Před 3 lety +11

    This is most certainly the easiest video to understand about partition. I literally am mad it's this easy haha!! Thank you so much!!! I definitely did NOT study this the day before my test :P

    • @mtophir
      @mtophir  Před 3 lety +7

      Thank you. I'm glad you find it useful.

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

    This is the correct way every human being can get crystal clear understanding of hardest topic easily
    Respect to great teacher like you sir
    love & Respect from India

    • @mtophir
      @mtophir  Před 2 lety

      Thank you and glad that you found the video useful. :)

  • @igorf243
    @igorf243 Před rokem +20

    It looks like this guy knows how to teach, awesome vid.

    • @mtophir
      @mtophir  Před rokem +4

      Thanks :) I try my best ...

  • @rew23able
    @rew23able Před 6 lety +19

    Best explanation ever.
    Very clear and crisp explanation and the representation helps to literally visualize the problem.
    Thank you !

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

    this is by far the best explanation of quicksort so far after pouring through countless videos.

  • @sakumas1183
    @sakumas1183 Před rokem +5

    thank you for this video!! i love how you used the cups, it's so much easier seeing this happen in real life instead of a digital diagram/images on screen.

  • @i2Chinese
    @i2Chinese Před 7 lety +12

    At 4:06 (when the partitioning is finished) there was a shift in the array which is very expensive, for the purpose of the quicksort I believe it'd be a more beneficial if you instead swapped the pivot with (i + 1)

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

    I struggled fully grasping this concept of why this works, you explained it very well and made the click that when I look back I don't understand where my confusion came through. Thank you so much.

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

      You're most welcome. Good to know that this video had helped you overcome your struggles.

  • @-kindredeternalhunter9907

    You are honestly good . Ur way is the best way to explain this, dunno why people keep explain this by code, all viewer want to know is the idea to do q sort ans you did it well

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

      Glad you liked it. Thank you so much 😀

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

    I wish our college lecture had explained it this clearly. College is where we go to get grades; CZcams is where we come to learn. Thanks KC Ang.

  • @JaBoss397
    @JaBoss397 Před 3 lety +9

    thank you again, I have spent my day trying to learn quick sort and this video actually gave a clear understanding to a point I can implement code with no problem. Thank you !!!!

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

      Thank you for watching this video and leaving a comment!

  • @DataStructures
    @DataStructures Před rokem +29

    Amazing, short, easy explanation

    • @mtophir
      @mtophir  Před rokem +9

      Happy to note you find the explanation easy to understand

  • @Honest_Reply900
    @Honest_Reply900 Před měsícem +4

    One of the best explanations, you nailed it

    • @mtophir
      @mtophir  Před 28 dny +1

      Thanks for the compliments!

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

    I've been searching like a hour for a clear explanation. Thank you! You are a life saver.

  • @hidroklorotiazid
    @hidroklorotiazid Před rokem +8

    This is the most clear explanation I have ever seen of the algorithm. Thank you so much.

    • @mtophir
      @mtophir  Před rokem +5

      Glad it was helpful!

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

    Incredibly helpful. Textbooks and online articles handwaved over partitioning. Thank you for explaining this so well.

  • @01binaryboy
    @01binaryboy Před rokem +5

    I went though so many videos. This is where I have understood the logic behind this algorithm very well. Thank you so much Master.

    • @mtophir
      @mtophir  Před rokem +3

      Thanks for the positive comment!

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

    Great explanation! One thing to mention is, instead of shifting everything from i+1 to the right, you can just do a swap of A[i+1] and pivot, (4 and 8 in this case) which is probably more efficient.

  • @Mr.SuperMan
    @Mr.SuperMan Před 8 lety +2

    The loveliest explanation of partitioning ever made.
    Hats off to you man!

  • @harryc5097
    @harryc5097 Před 4 lety

    Best demonstration I have seen for quick sorts. I'm a visual learner and using real world objects really helped, not to mention the great instruction and organized narration.

  • @eucliwoodhellscythe4324
    @eucliwoodhellscythe4324 Před 3 lety +7

    the best explanation of quick sort thank you

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

      Glad you liked it!

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

      @@mtophir wish me luck bro, im going to face interview in this week 🙏

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

      @@eucliwoodhellscythe4324 Good luck! All the best!

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

    Agreed with previous person. The most clear description of Quicksort I've seen. Well done and thank you.

  • @kentnixon1651
    @kentnixon1651 Před 4 lety

    Was having a lot of trouble understanding this algorithm until I stopped here and watched your video - now it totally makes sense! Thank you for taking the time to not only make the video, but to actually demonstrate the algorithm in physical form. It really helped me to learn much more concretely than any of the code snippets other tutorials were relying on.

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

    It's amazing how much better this tutorial is than the other ones out there. Especially from a non-native English speaker. I applaud you.

  • @amrsaber5457
    @amrsaber5457 Před 8 lety +24

    at the last step you should swap not shift ... shifting means moving all the next objects and that would be with no purpose ... but swapping does the work simply with no extra efforts

  • @crepkey
    @crepkey Před 4 lety +3

    This is the first explanation that I understand immediately after I've watched it! Thank you so much! Greetings from Hungary :)

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

    This is single handedly the best visualization of quicksort I've ever seen. Thanks for this!

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

      That's a very nice compliment. Thank you!

  • @spioboy1
    @spioboy1 Před 3 lety +4

    This is the best explanation of Partitioning on CZcams. Thank you!

    • @mtophir
      @mtophir  Před 3 lety

      Thank you~ glad you liked it.

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

    Wow...the way you used the cups for explaning the concept is so creative

  • @giovannicarovanello62
    @giovannicarovanello62 Před 7 lety +5

    I finally understood QuickSort(A[],first,last). Thank you very much. You video was very illuminating!

  • @bilalkhanthedreamer
    @bilalkhanthedreamer Před 6 lety

    You're great sir. I was trying to understand quick sort from many days, but you made me understand the working of quick sort partition in less than 5 minutes. Thank you so much.

  • @user-mj1lu4cl1j
    @user-mj1lu4cl1j Před rokem +1

    After going through so many contents and CZcams videos I finally found someone explaining the Quicksort in a very clear manner. Brilliant job KC...!👌

  • @Moch117
    @Moch117 Před 11 měsíci +5

    Watched so many quicksort videos but this one is the best. Thank you good sir !

    • @mtophir
      @mtophir  Před 11 měsíci

      Glad you liked it!

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

    Just Awesome !!!! Crystal Clear !! Tried a few lectures!
    This is GOLD!
    👍❤️

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

    Thank you so much! This is the only explaination I can understand. No those fancy graphic animation and music, no confusing complex definition and pesudo code, just a straight forward visual explanation. After I watched this, the pesudo code suddenly makes sense now.

    • @mtophir
      @mtophir  Před 3 lety

      You're welcome and thank you for the kind words.

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

    I believ i see your video everytime i need to refresh about quick sort. Thank you. I may have seen it more than 15 times !

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

    he explains complex stuff in a very plain way. What an illustration!

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

    The best explanation of partitioning I have ever seen.

    • @mtophir
      @mtophir  Před 2 lety

      Glad you think so. Thanks!

  • @courses-lectures
    @courses-lectures Před 2 lety +2

    You are an amazing instructor ... its is considered the best tutorial in quick sort algorithm

    • @mtophir
      @mtophir  Před 2 lety

      Thank you for watching and for acknowledging the effort!

  • @pastafarian8410
    @pastafarian8410 Před 4 lety

    This is the best explanation of Quicksort I 've come across anywhere. Cheers

  • @jogofwar99
    @jogofwar99 Před 8 lety +5

    Very smart implementation Mr. Ang!

  • @mukul98s
    @mukul98s Před rokem +5

    This is the best video on quick sort I have watched . Thanks man for making this amazing video.

    • @mtophir
      @mtophir  Před rokem +2

      Thank you. Glad you liked it!

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

    you make so much sense, the hardest part of quick sort is understanding the first passing of the partitioning function. Thank you

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

      Hope the visualisation in this video has made "the hardest part" a bit easier to understand.

  • @yolamontalvan9502
    @yolamontalvan9502 Před 11 měsíci +2

    WOW, I MEAN WOW. This is the best explanation of the QuickSort I have seen. Not even AI help could have shown that. Thank you.

  • @kevinvira
    @kevinvira Před 6 lety +14

    Finally I know how QuickSort works. Thank you.

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

    OMG, your explanation makes a lot of sense compared to other videos i've seen, NOW I GET IT>>>>>>>>>>>>>>>> THANKS

  • @georgesmith9178
    @georgesmith9178 Před 4 lety

    The best part was that you actually pointed me to the fact the (i) pointer is initially at (low -1) and that it will not be out of bound because it will be incremented just before the first swap. Special thanks for that.

  • @amadousallah6634
    @amadousallah6634 Před 7 lety +2

    Wow! What a great instructor. This is brilliant. Thank you so much for the amazing explanation.

  • @sulaymonolimov7798
    @sulaymonolimov7798 Před 10 měsíci +21

    Amazing explanation!

    • @mtophir
      @mtophir  Před 10 měsíci +1

      Glad you think so!

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

    The kind of teachers we students crave for!!....Thanks Sir

  • @user-tz7vl4no6o
    @user-tz7vl4no6o Před 7 lety

    The video describes the basic unit of computation in the processing of quick sort algorithm. Very clear and beautiful!

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

    Thank you so much for a simple and wonderful explanation of partition algorithm for QuickSort. Beautiful!!

  • @brahmdeepsingh5246
    @brahmdeepsingh5246 Před rokem +5

    I already know quick sort but came here for the awesome explanation 👏🏼 its real simple and cool.

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

    Best explanation ever of quicksort

    • @mtophir
      @mtophir  Před 2 lety

      Thank you, and thanks for watching.

  • @marcelcohen4844
    @marcelcohen4844 Před 6 lety

    I've been watching about 20 videos of quicksort, this is only one that explains the concept clearly

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

    One of the beast explanation of partition. You can also select i (=j) at 1st position and increment after swap, final move swap pivot with i+1 Thanks

  • @vlads7774
    @vlads7774 Před rokem +3

    the best explanation on this type of sorting, thank you a lot !

  • @blackmamba9950
    @blackmamba9950 Před rokem +3

    The best example I have seen on the internet. Thank you Sir

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

      That's a great compliment! Thank you.

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

    thank u sir....this is the most clear explanation than most of other explanations of quick sort in the youtube.

  • @SaurabhKr
    @SaurabhKr Před 7 lety +2

    One of the best simulations of the partition procedure, thanks for making this video :)
    I hope to see more of your video.
    Thanks

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

    Best video I've seen yet... keep up the good work!

  • @globnomulous
    @globnomulous Před 3 lety +4

    Thanks for posting this. I found it enormously helpful.

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

      You're most welcome!

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

    Thank you for your professionalism, Tom.

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

    The only clear visual explanation I was able to find - thank you :)

  • @diegoromo4819
    @diegoromo4819 Před 10 měsíci +3

    I have to admit there is a lot of great videos out there for data structure and sorting videos, but loved this one!

    • @mtophir
      @mtophir  Před 10 měsíci +1

      Thank you so much for the compliment!

  • @shieldanime6968
    @shieldanime6968 Před 10 měsíci +6

    please make more videos like this coz it is easy to understand. Thank you sir. 🎉🎉🎉👍👍👍❤️

  • @memeingthroughenglish7221

    clever way of sorting the array and demonstrating how the i and j move through the loop together and make the pivot! Thank you so much for your efforts!!

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

    So simple and elegant, straight to the point. Much better than those overdone graphics you see in other channels.

    • @mtophir
      @mtophir  Před 3 lety

      Thank you for the kind words!

  • @kyu9649
    @kyu9649 Před 7 lety +3

    Extremly well explained ! Very good video !

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

    Very clear explanation! Wish all of my proffessors had this impact

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

      Thank you for your kind words

  • @nanzownzu
    @nanzownzu Před 3 lety

    Perfect. Wanted to refresh my memory and saw this. I knew I did not need to see any pseudocode or code but just wanted to understand the process. This was perfect.

  • @ayra_2001
    @ayra_2001 Před rokem +2

    I have been struggling for a few days to get a clear idea of quick sort and sir you have made it super clear. Thanks a lot, sir💜
    May God always bless you:>

    • @mtophir
      @mtophir  Před rokem +2

      Happy to know that the video was helpful. All the best!

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

    Great video, thank you so much. The book and examples made no sense, but this is a lot better than the examples online.

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

    it's an awesome explanation that clarifies many subtle moments to implementation. and even more, this trick with the last step!! ahahaha! I wanted to scream about p->i+1. sure, you did it intentionally for better memorizing)) amazing lesson. thanks from russia

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

    WOW, I watched green animation video, and was giving up on quick sort, and then watched your video and its as clear as day.

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

    He explained in 5 minutes what other videos didn’t in 10-15 minutes. Best quicksort video I’ve seen

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

      Thank you for your vote of confidence.