Quicksort In Python Explained (With Example And Code)

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

КОМЕНТАРІ •

  • @zaeemjaved6850
    @zaeemjaved6850 2 роки тому +30

    Of all the videos I watched on youtube for quick sort, I understood this one a lot better in a much more intuitive way.

  • @1Lia11
    @1Lia11 7 місяців тому +4

    i've been searching for hours for a video that could explain exactly what happens when there is only 2 elements, and i finally got it! thanks a lot

  • @stumbleio
    @stumbleio 11 місяців тому +1

    Best explanation on UA-cam. You should continue making these types of videos. You do such a great job at it!

  • @sweetiepiedpiperr
    @sweetiepiedpiperr 4 місяці тому +1

    I can't like this video enough!!! Danke schön! You explain so well -- I've finally understood Quicksort!

  • @MaleeshaHuththo
    @MaleeshaHuththo 3 роки тому +16

    this entire playlist saved me great deal of time. thank you very much!

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

    Very best explanation of the quick sort. Thank you. Please ignore the Dislikes and Keep it up bro. You did an amazing job to clear the understanding of this algorithm. Thank you again!

  • @SourabhGurav-s4o
    @SourabhGurav-s4o 15 днів тому

    Thanks Felix. The explanation was slow and intuitive. Understood every bit of what you explained.

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

    Bro you gave me full understanding of quick sort! Thank you! Please continue to make such videos with other sortings in Python!

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

    one of the finest explanations found on youtube.

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

    Thanks a million for making this algorithm easy to understand.

  • @henryconner780
    @henryconner780 7 місяців тому +2

    Thanks bro, really great visual. You saved me a lot of time

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

    LOVE THE SWAP ANIMATION!

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

    The graphic explanation is perfect.

  • @andrew15_5
    @andrew15_5 5 місяців тому +1

    9:46 You forgot to say "- 1" after "right will be the length of the array" (since left starts at 0 and not 1).
    The explanation is great! I forgot about how this works after several years of no practice, but now I remembered everything!

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

    Masha"Allah Felix, This took me a day to understand, you video helped me alot

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

    what ever your reason was, that made you stop making videos. 1. Thanks for making them, really well explained! Hut ab!
    2. The market for tutorial creators might seem "overcrowded" but really sad that you stopped, I think you have great potential!
    Have a good life, mate! 🤘

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

      Vielen vielen Dank für die nette Worte. Ich hatte wenig Zeit in den letzten Jahren, aber ich plane ab 2024 weiterzumachen :) Grüße nach Indonesien :)

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

      @@FelixTechTips dann wünsche ich dir einen starken Start im neuen Jahr! 💪🏻🤘🏻🤘🏻🤘🏻🤘🏻😁

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

      @@FelixTechTips Really eager to get your videos more and more on algorithms and coding.

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

    This is indeed a great explanation.Thanks a ton.

  • @liabasqulizad7962
    @liabasqulizad7962 9 місяців тому +1

    Thank you sir. That was very good explanation.

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

    That was an amazing way of explaining quick sort thanks

  • @TheKorkahn
    @TheKorkahn 4 роки тому +2

    nice smoothing voice, thanks for the explanation FelixTechTips!!

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

    At 11:08, you say "the j that defines the point right of the pivot," but if the pivot is always the last element in the array, nothing can be to its right. Did you mean "the point left of the pivot"?

  • @Mc-os1yc
    @Mc-os1yc 3 роки тому +29

    In your visual explanation, you mentioned a scenario where j < i. That will never happen in your code because both while loops break when i=j. Anyway, it's a good vid nonetheless. thanks

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

      I don't think that's right. Only the outer loop will break when i=j

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

      Yes, I also found this discrepancy. Probably just an omission on author's side. But in general - wonderful explanation, thanks a lot!

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

    Could we also make the right endpoint just the very end of the array and the left at index 0? Or how would we do it if a different pivot was used?

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

    Incredible explanation!

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

    when i does not get incremented if the while loop does not run at all initially, that then creates a negative -1 right parameter in the quicksort(arr, left, partition_pos-1) ,where partition_pos=0, function later on. Why isn't this a problem?

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

    I'm having trouble understanding one thing. After i and j meet, shouldn't we just unconditionally exchange arr[i] and the pivot? Why do we check that arr[i] > arr[right] at that point? Shouldn't the pivot be placed at that point that i and j meet in order to be properly sorted?

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

      Same doubt bro

    • @jacqueline2330
      @jacqueline2330 2 роки тому +7

      i think the check is considering the case that only TWO elements were left. we always pick up the last element as our pivot, and the remaining one will become the one=i=j automatically. so we should check it first before we swap them. Like in the video 8:27, what if we have 77 88 left already inplace instead of 88 77?

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

    man thanks a lot even though i don't get it well yet but i will rewatch again

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

    Thank you for so comprehensive explanation

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

    Why don't we use "if array[i] > pivot and i > j "# indicating that they have crossed each other instead of "if array[i] > pivot:" since our objective is to handle the scenario where they cross each other?

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

    In your example there was no scene where you swapped the elements at I and j position then why did you do so in code?

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

    cual es la condicion de parada para la recursion?

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

    Very good explanation

  • @Plum-v6p
    @Plum-v6p 11 місяців тому

    Hey !your explanation is great.one quick question
    Python has line by line interpreter
    So,when we call partition function won't that cause declaration error because it should be declared first before calling...

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

    Instead of passing list as input, if tuple, dictionary, set is passed. So will the time complexity of the algorithm remains same or will differ?
    Thanks,

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

      Now this won't work on tuple i suppose because tuples are immutable and in this algorithm we swap and make changes in place itself. So.. Nope it won't work for tuples.it would give us error... So it's time complexity might not be there with tuples.

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

    you saved my time thank you

  • @ahmedel-sinousy4848
    @ahmedel-sinousy4848 3 місяці тому

    thank you for explanation but I'm looking for in-place implementation for the quick sort because it's an in-place one

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

    Thank you! Your explanations were very good. How would I change the code for parallel quickSort? Is dynamic parallel quickSort the same as just parallel quickSort?

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

      I’m not sure what this means but this sounds like you could know the answer to this. Could we also make the right endpoint just the very end of the array and the left at index 0? Or how would we do it if a different pivot was used?

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

    i faced a problem in it that quick sort isn't quick with me at all
    idk why it doesn't work i guess my laptop is kinda weak

  • @Hex-Scholar
    @Hex-Scholar Рік тому

    Could you please explain me what will the space complexity be ?

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

      I may be late but Quicksort has O(logn) space complexity. This is due to the algorithm recursively calling itself which takes up space in memory because it must remember where it was in the previous levels of recursion. Because Quicksort on average has log base (4/3) of n recursion levels as that is the average partition for Quicksort and log base (4/3) of n is in O(logn), that is its space complexity.

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

    For me, i found another way to do the partitioning using a for loop in python
    def partition(arr, high, low=0):
    if not arr and not high:
    return 'No array and no upperbound'
    pivot = arr[high]
    partion_at = low
    for j in range(low, high):
    if arr[j]

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

    Can you please upload the quick sort for worst case scenario with O(n^2) ?

    • @davidr.603
      @davidr.603 Рік тому

      That happena when the pivot element is either really big or really small not sure which it was, maybe both

  • @VladislavRashkov-e6w
    @VladislavRashkov-e6w 4 місяці тому +1

    Thank you!

  • @AM-nl5yo
    @AM-nl5yo 2 роки тому

    Thank you, this helps me ✨

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

    Thanks for the wonderful video. The code was not worked for me. It went infinite loop. I fixed the issue by adding i += 1 and j -= 1 after arr[i], arr[j] = arr[j], arr[i]

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

    Thank you so much for the explanation!
    By the way, how should this algorithm be implemented with random element chosen as the pivot?

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

      we could use any element as pivot in this case he has taken the last one

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

      @@anirudhsoni6529would we put the right pointer at the very end of the array instead of how he has it here, where it is just left of the last element?

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

    Please why is J right -1?

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

      right means size of array i.e, 8
      here index will start from 0th so array[last_ment] =>array[8]
      it means we dont have 8th element in array
      thats why right -1

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

    j can be less than i???

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

    Sirr where r urrr Videos... it's been a yearr...Did u quit???? 😭😭😭

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

    Super.....but ur code should be zoomed it will look better anyway nice ❤️

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

    What if there are duplicates?

  • @user-qb5tg6eg2e
    @user-qb5tg6eg2e 3 роки тому +1

    13:36 my own video mark

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

    I got it thank you

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

    Thank you so much

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

    Nice animations!

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

    Why return i?

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

    Danke bro!

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

    good works

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

    Cool!

  • @Anjalikumari-cl7zx
    @Anjalikumari-cl7zx 18 днів тому

    please made more videos on other topic also please

  • @THE-MNG
    @THE-MNG 3 роки тому +1

    I got an error when I use 100 elements of array

    • @Mc-os1yc
      @Mc-os1yc 3 роки тому

      I suppose it's stack overflow cuz it hits stack size of your system

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

    Dadu pashu 😍😍😍😍

  • @andresc.56
    @andresc.56 Рік тому

    ur awesome

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

    If you run the sorting function, the result is None. This is because the sorting function does not Return anything.
    You may want to edit your code.

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

      works fine for me. It isn't returning anything because this algorithm is designed to be in-place and it is just modifying the original array itself. print your array, call quick_sort(), then print your same array again and you will see it sorted. If you want, you can add return arr at the end of your quick_sort function, then in your main call it using sorted_array = quick_sort(), but it's not necessary.

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

      it will work just fine cause he is sending list as a parameter and that is passed by reference

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

    unbound local partition error

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

    Kalander op

  • @matissjansons8789
    @matissjansons8789 11 місяців тому +1

    how about this:
    def quicksort(arr):
    if len(arr) pivot]
    return quicksort(left) + middle + quicksort(right)

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

    I think it's ' j= right '

  • @wannabehuman.production
    @wannabehuman.production 6 місяців тому

    May you pillow be always cold on both sides

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

    Qulandar

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

    Bro this could have been done with a single while loop, why increase time complexity. Nice vdo but you lost me there

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

      the program operates exactly how he explained it , not a single instruction more , tell me

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

    Dadu

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

    ok, I posted a comment before that was incorrect. I was missing a piece of code - Works wonderfully - Here is another example in python of quickSort application
    Check my function below and works for any example of an array.
    def quickSort(dataset, first, last):
    if first < last:
    pivotIdx = partition(dataset, first, last)
    # now sort the two partitions
    quickSort(dataset, first, pivotIdx-1)
    quickSort(dataset, pivotIdx+1, last)
    def partition(datavalues, first, last):
    # choose the first item as the pivot value
    pivotvalue = datavalues[first]
    # establish the upper and lower indexes
    lower = first + 1
    upper = last
    # start searching for the crossing point
    done = False
    while not done:
    # TODO: advance the lower index
    while lower = lower:
    upper -= 1
    # TODO: if the two indexes cross, we have found the split point
    if upper < lower:
    done = True
    else: # if they haven't cross each other
    temp = datavalues[lower]
    datavalues[lower] = datavalues[upper]
    datavalues[upper] = temp
    temp = datavalues[first]
    datavalues[first] = datavalues[upper]
    datavalues[upper] = temp
    # return the split point index
    return upper
    # test the merge sort with data
    print(items)
    quickSort(items, 0, len(items)-1)
    print(items)