Algorithms: Quicksort

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2024

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

  • @cheyno237
    @cheyno237 6 ปีที่แล้ว +2256

    Every single quick sort video uses a different method, and they all do at least one weird thing without explaining. I'm not even sure if quick sort is a real thing at this point :)

    • @A.K.00
      @A.K.00 6 ปีที่แล้ว +7

      Cheyno Mdingi Lol

    • @j.f.m.4265
      @j.f.m.4265 6 ปีที่แล้ว +73

      So true, I'm having the same problem ahah

    • @OneTimeCrazy
      @OneTimeCrazy 6 ปีที่แล้ว +12

      the smiley face doesn't make much sense in this context. I think that it all does the same thing just some methods keep the same array while others split it into different ones using recursion/multidimensionalarrays. Regardless, it appears that these two methods depend on what kind of approach you're taking -- loops or recursion.

    • @xiaotianlu2842
      @xiaotianlu2842 6 ปีที่แล้ว +7

      This is the method that I was taught at Uni.

    • @malecrium8665
      @malecrium8665 6 ปีที่แล้ว +15

      @@OneTimeCrazy smiley face makes sense but a laughing face XD would be better received by more people I guess.

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

    Who here only 15 year professional development experience and I still only use these algorithms in interviews.

    • @hujake5406
      @hujake5406 4 ปีที่แล้ว +24

      I watched over 40 videos about this algorithm. I still have no clue.

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

      @@hujake5406 ouch

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

      Depends if ur more soft eng or work in theoretical computer science

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

      @@IStMl I'm learning computer science by myself. Do I need to be good at and understand every algorithms? I do understand them but I can't write them by myself without researching on the internet.

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

      @@hujake5406 always check explanation and code in parallel

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

    I understood the video at first. But, then I went into the comment section

  • @emilybjoerk
    @emilybjoerk 7 ปีที่แล้ว +451

    You should not compute the middle point as (left + right)/2. If the array is very large (>2G) then the result of "left + right" may overflow and become negative. The proper way of choosing the midpoint pivot is: "left + (right-left)/2" which is mathematically equivalent but immune to overflow as "right > left" is an invariant that always holds and if "right" is representable then, "left" is also representable the result will never overflow. as it will be less than "right".

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

      Emily Björk i

    • @MartinAcevedo
      @MartinAcevedo 7 ปีที่แล้ว +36

      It was a bug that was in C library for 30 years until someone discovered it due to an error using big data.

    • @NeMayful
      @NeMayful 7 ปีที่แล้ว +11

      this is a big plus in the interview

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

      Very good point ma'am.

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

      A much appreciated tip!

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

    IMPORTANT: the solution from the code in the video does sort the array, while the algorithm is wrong actually. It's very "quicksort", but it's not standard quicksort. To verify my point, you can run a demo in your IDE with debug mode, and watch how the pointers moving, and how the pivot moving. As many comments mentioned, the explanation and the code CONFUSING people. I won't suggest beginners waste time watching this video.

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

      Don't even know why he's using a pointer. Just make 2 arrays 1 for left and 1 for right and let them sort by recursion.

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

      @@anonymunsichtbar3715 I think that if you have a very large array It should use a lot of memory and we don't want that, but its a good point from you

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

    1:56 You said everything bigger than pivot (pivot = 7) is now at right side and everything smaller than pivot is now at left side of pivot. But 8 is still leftside of 7. Why?

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

      Lol my thoughts exactly... Went straight to the comments to see if I was crazy or not

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

      No, it means everything smaller than the pivot is in a left section and everything larger than the pivot is after that left section. The dividing index between the two sections does NOT need to be the pivot itself.

  • @rkcst6503
    @rkcst6503 7 ปีที่แล้ว +745

    Hmm... all these years I thought 8 was bigger than 7.

    • @doctor3397
      @doctor3397 7 ปีที่แล้ว +81

      I like Gayle... But 8 should really be swapped with the pivot 7..... This tutorial is VERY confusing....

    • @doktoren99
      @doktoren99 7 ปีที่แล้ว +111

      Bros, the video is actually correct. Ill attempt to clarify, but i have a mild form of downs syndrome so please be gentle.
      The pivot, (i like to call it the pivotValue for clarification), is just a number she selected randomly. In her code she just extracts the value at the arrays middle index, and its just a number you will compare all the other numbers to :)
      Now for part two, she makes two pointers, these move towards each other, which kind of separates all the values into two piles "bigger than or equal to pivotValue: 7" and "smaller than pivotValue: 7".. And thats what you are seeing when the two arrows meet. The meeting point of the two arrows are kind of like a wall, separating the two piles, which will then be treated separately, and each of them will have quicksort() performed on them later.
      So in the right pile, you have "7 and greater", and in the left pile, you have "smaller than 7".
      And now the fun starts all over, the calls quicksort() on the left pile, and then right pile.

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

      @Alexander K. Thanks that helped a lot

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

      @@doktoren99 That was a brilliant explanation!

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

      "All elements less than 7 should be before all elements greater than 7" is what she said which means its all true

  • @saqueebabdullah9142
    @saqueebabdullah9142 7 ปีที่แล้ว +345

    At 1.48min you said everything smaller than one side and bigger than other side of the pivot, where there is 8 exactly left to 7 which is pivot. Please explain.....

    • @aaroldaaroldson708
      @aaroldaaroldson708 7 ปีที่แล้ว +59

      ERROR 404

    • @TheNargPanac
      @TheNargPanac 7 ปีที่แล้ว +24

      she's saying that you can cut the array in two parts with each part containing only numbers smaller/greater than the pivot. in this case you could cut between 5 and 8 :)

    • @joelschulz-andres6651
      @joelschulz-andres6651 7 ปีที่แล้ว +51

      The index of our pivot element is NOT our separating index. Which only makes sense, because we are assuming a randomly sorted array, so the randomly chosen value could also be the biggest in our whole dataset. Instead we'll keep moving the left and right index until left > right, which means that left has run over right. The right and left partitions are now still unsorted, which is the point of the quicksort algorithm. I hope this helps.

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

      Looks like this video mixes pivot definition/purpose from one partition scheme with implementation of another one. Thus the confusion. en.wikipedia.org/wiki/Quicksort#Lomuto_partition_scheme

    • @juaneugenioabadie1594
      @juaneugenioabadie1594 7 ปีที่แล้ว +8

      The algorithm states that after each partinioining, the pivot will go into his final position, and that's not the case there. source: algs4.cs.princeton.edu/23quicksort/, en.wikipedia.org/wiki/Quicksort

  • @DyslexicAnaboko
    @DyslexicAnaboko 6 ปีที่แล้ว +7

    I just want to point out that the book example is completely different. Additionally I think it will be useful for people watching this to know that the "random" part of this isn't the index being chosen because that is not random, it's always the middle of the array. The value found in the middle of the array is what is unknown and random hence the O(n^2) possibility. I just wanted to make that very clear.

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

    Although this implementation and explanation was quite clear to me (though I'm seeing this as revision not learning it first time), I do understand why it may be confusing for new learners watching this, so just 2 things that I didn't think was explained well to hopefully clarify some people:
    1. Pivot is NOT THE SAME as the partition point where you choose where to split the array in your current iteration: In the initial video explanation, don't be mistaken thinking that because the pivot is 7, then it is also where the partition is split, therefore everything before 7 should be less, and everything after 7 should be bigger. Also, the video is not wrong when it did not swap 8 and 7 (I see a lot of complaining comments on this), because it's really just up to the implementation itself. It could well be if right index >= pivot then keep moving, so since 7 == 7 then right index keep moving (and it does look like it since you see it sets partition point between 5 and 8, where everything before 5 is less than pivot and everything after 8 is equal to or greater than pivot, as there can always be duplicate numbers in the array and that could be the pivot number itself).
    2. What you return as the partition index in the partition method DEPENDS ON YOUR IMPLEMENTATION: Since the point where you partition the arrays is in between two elements, it's completely up to you on how you want to represent this point in the array. In this case, the partition method is returning left because the while loop only exits when left and right cross-over each other, meaning left will be the index of the BEGINNING of the right-hand-side partition, and right will be the index of the END of the left-hand-side partition, and in the quickSort recursive method it calls to quickSort the left-hand-side partition with boundaries between left to "index-1", and right-hand-side partition between "index" to right. By all means, in partition method, you could choose to return right instead of left, then simply have quickSort call left to "index", and "index+1" to right. Or do some other kind of calculation to get your partition index, It is completely up to you.
    Hope this helps!

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

      Thanks, the pivot versus partition point was what got me in this one. I saw the 8 go to the left of 7 and then I got lost from there on out. But having the partition point where the 2 pointers meet makes total sense.

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

      The animation and her implementation are different. In her implementation she has while arr[right]>pivot, keep moving but in the animation the condition is arr[right]>=pivot.

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

      No, 7 is not in its final position, thereforce the algorithm they used is wrong. After each iteration of the sort, one entry must be in its final position.

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

      Actually, her animation is right. Even though the condition is arr[right]>pivot ( and arr[left]=pivot on one side and

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

      @@tomleeyrc40 That's not necessary under this quicksort implementation.
      After each iteration the array is partitioned in two halves with respect to the pivot. the pivot can fall anywhere in the two halves, what matter is that everything less(or equal) is on the left half and everything greater(or equal) on the right half.
      Repeat recursively for each halves until the size==1 and the array is sorted.

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

    GitHub copilot sent me from inside VS Code when I was asking about quicksort, commented with the link. What a time to be alive!

  • @johndunne3983
    @johndunne3983 7 ปีที่แล้ว +32

    public static void swap(int [] array, int left, int right){
    int temp =0;
    temp= array[right];
    array[right]= array[left];
    array[left]= temp;
    }

    • @HillChris1234
      @HillChris1234 7 ปีที่แล้ว +9

      array[left] = array[left] + array[right];
      array[right] = array[left] - array[right];
      array[left] = array[left] - array[right];

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

      Hell yeah thanks man

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

      Christine Hill
      array[left] ^= array[right];
      array[right] ^= array[left];
      array[left] ^= array[right];

  • @ashishdhupkar5065
    @ashishdhupkar5065 6 ปีที่แล้ว +7

    code will work well if made following changes
    while (left < right)
    {
    while (arr[left] < pivot)
    left++;
    while (arr[right] > pivot)
    right--;
    if (left < right) {
    swap(&arr[left], &arr[right]);
    }
    }

    • @Manu-wb2uv
      @Manu-wb2uv 5 ปีที่แล้ว

      It works either way

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

    This video is flawed in multiple details:
    1) it is never clearly mentioned when a partition is "sorted".
    2) it is never clearly mentioned how the new partition boundaries are picked.
    3) you do not clearly distinguish between pivot and separation index.
    What I like though is that you actually code it and explain what you do.

  • @carlosromero-sn9nm
    @carlosromero-sn9nm ปีที่แล้ว +1

    This does not show what the code is doing, it's describing what's happening in the algorithm but from abstract overview. As a coder I like seeing both an abstract outlook but also explanations of the inner workings by desk-checking and tracing the execution of the code.

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

      She's just a dumb privileged Tool

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

    I think the error is not with the sorting of the values -- that is perfectly correct.
    The error is with where the left and right indices have ended. Left and right should criss-cross (at least from the code from my University notes) so in the first partition left ends at index 5 and right at index 4.
    This is further validated if you reuse this idea for the quick select algorithm.

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

    Thanks for the code. I think it works. However, the aesthetics in this video don't make up for a bad explanation.

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

    I think at 1:45 it missed a step which is swaping "8" with the pivot"7"

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

    Please run your code to make sure it works before publishing an instructional video.

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

    I've been trying to understand it for the last few months because the book I have is terrible at explaining things. Now it all make sense.

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

    Totally confused by this explanation. I haven't written a quicksort in about two months, so I forgot how. But this is just confusing.

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

    Is that really correct? I know after first sort , left side elements must be smaller than pivot and right side elements must be larger than pivot element but in this example, you have 8 still at left side and not well separated. So your code must compare pivot and 8 and swap them too

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

      This confuses me as well.

  • @k.arslan7959
    @k.arslan7959 6 ปีที่แล้ว +4

    I understood at the first time watching the video. It ve never happened before with the other algorithm video explanations so I think this channel deserves a good thank you.
    Best wishes, thumbs up

  • @flying-musk
    @flying-musk 5 ปีที่แล้ว +13

    is there anything wrong in line 32???
    I think the condition of the "if function" should be (left>right)

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

      no, because you still want to swap when "left" index is smaller than "right" index. Technically speaking, you should take out equal sign and just have it "

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

      ​@@CJKims that's actually incorrect. I ran this myself and took out the equal sign and the outer while loop ran infinitely long. It's understandable, because imagine at the end of a partition, we have left = right. And the all the left sides have been sorted, and all the right sides have also been sorted (in the sense that they are smaller/greater than the pivot). Now, the outer while loop will keep continuing, since left

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

    Nice video, does this actually work from observation it seems like it would break for an input [2,3,1] in the line of code that says "while(array[left] < pivot)" or when pivot is the largest element this would raise an index out of bounds error would it not?

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

    I pretty much understand this video, except the if (left >= right) return ... and she keeps saying "if left is greater than right, just return; there's nothing to do" ... how is there nothing to do when those are out of order? I'm sure I'm missing something but it's a bizarre comment

  • @timcook3410
    @timcook3410 7 ปีที่แล้ว +24

    mistake in a hackerrank video! this is the day world ends!

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

    In second iteration, when we sort {6,3,1,2,5} i understand we swap 6 with 2. According to the code provided later, after the swap the left pointer moves ahead while the right moves back. However, in the video only the left pointer is moved ahead, while the right pointer remained in place...
    I think if the right pointer is moved back as well as it should be, then in next iteration, 3 and 1 would get swapped resulting in {2,1,3,6,5} while the next sub-arrays would be {2,1} and {3,6,5}...
    Plz correct if wrong

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

    In the real world its actually very practical to implement your own algorithms. Take Doom 3 for example. The developers implemented all their own libraries to ensure the code ran as fast as possible with minimal file sizes. This was very important as if they used existing libraries the code would of took up much more space and may of even prevented it from running on most consoles back in the day. This is also the case for embedded os with low computation power. Also 8 is greater than 7...

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

    you forgot one of the basic step in there which lead to this all confusion among the viewers

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

    I don't think the partition part will work if there are duplicates in the array

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

    for those watching, (left + right) / 2 can cause overflow. the author surely knows this, but might've assumed others do too. It is safer to do (left + (right - left) / 2) to avoid overflow.

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

    If the array was [3,4,7,5,1,2] wouldn't you get stuck in infinite recursion?

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

    she is saying everything bigger then pivot is on one side and everything smaller then pivot is on the other side. that's really confusing.!! in 1:51, the pivot is 7 and yet 8 is on the left side of the pivot instead of the right side.. same thing at 0:38 where the pivot 7 after the value of 9.

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

      You misunderstood this method. She said everything bigger than 7 should be on 'one side', not on the right side of 7. Feel the difference.

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

      ​@@kevincui1631 She said everything bigger than 7 should be on 'one side'.... erm let me rephrase then.. why there are bigger numbers then 7 on both sides?

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

      Nusakan what do you mean by both sides? It looks like all the number greater than 7 are on one side to me.

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

      Paul Tan No. 7 and 8 shouldn’t be swapped !

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

      @@kevincui1631 1:51

  • @barisyakut7970
    @barisyakut7970 7 ปีที่แล้ว +72

    Comment section of this video is more useful than the video itself.

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

      true

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

      its called community! it helps!

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

    code is not working

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

    That's a bug inside the code. For example, it'll never stop if the input array is [6, 3, 1, 2, 5], and the pivot is 1.

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

    A working implementation similar to her theory in case anyone is wondering.
    .
    .
    function quicksort(arr, low, high){
    if(low < high){
    let p = partition(arr, low, high);
    quicksort(arr, low, p);
    quicksort(arr, p+1, high);
    }
    }
    function partition(arr, low, high){
    let pivot = arr[low + Math.floor((high - low)/2)];
    let l = low, h = high;
    while(true){
    while(arr[l] < pivot){
    l++;
    }
    while(arr[h] > pivot){
    h--;
    }
    if(l >= h) return h;
    // Swap low and high element
    [arr[l],arr[h]] = [arr[h],arr[l]];
    // In case it swapped repeated elements,
    // decrement higher indexes
    // so it will not go infinite loop.
    if(arr[l] === arr[h]) h--;
    }
    }

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

    The theory is correct but implementation is wrong
    while(arr[left] < pivot)
    {
    left++;
    }
    This may lead to array out of bound.

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

      G Edhaya Chandran It breaks when left index matches pivot index

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

    1:54 if 7 is the pivot, then after all the swappings, 'right' and 'pivot' should be swapped. Only then the 'pivot' will be in it's correct position, as per the algorithm.

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

    I thought this video explains this very well, especially after trying to understand the other videos on quicksort, the comments dont seem to know what they are talking about

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

    There are 2 while nested loops in the partition method, is the time complexity at best case still n log n for the implemented code ?

  • @SantinaLin424
    @SantinaLin424 8 ปีที่แล้ว +7

    Correct me if I'm wrong. I'm guessing you don't really need the check "if (left

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

      In this case, yes. Since pivot is (left + right) /2
      But suppose its random, then the left/right index will move beyond the pivot point. (I hope Im assuming that was the issue, and i explained the correct thing haha)

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

      Santina asked this question... and it's at that point the interviewer decided you can't be coached up, and therefore they can't work with you at this time. Isn't it fun?

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

      Yeah, I took the pivot element as the left index and it was not working if I didn't have that "if(left

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

      Santina DM me. You're hot and I need to know more about you. 你說中文嗎

  • @aaronsilver-pell411
    @aaronsilver-pell411 6 ปีที่แล้ว +1

    she has a swap method but doesn't have a swap function from what I can tell. I found this highly confusin.

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

    I am confused, as much as i learnt every element on the left side of the pivot must be smaller than the pivot and every element on the right of the pivot must be larger than the pivot. 8 > 7 yet you kept it on the left of the pivot.

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

    emmm.. wouldn't the first while loop within the while loop accumulate the left until it reaches < pivot (while doing nothing)? then move on to the next execution point? You sure this is right?

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

    How 2,1 becomes 1,2? I think it is the most complicated part..... Many teachers don't explain the last part, when only 2 components remain .

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

    For [2, 3, 1], what if we somehow chose 1 as pivot, it seems there is no swap, so it doesn’t seem to work.
    Also, we do we do with [6, 5]?

    • @Manu-wb2uv
      @Manu-wb2uv 5 ปีที่แล้ว

      It actually works just follow code. After the second partition ur array will be sorted

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

    This is honestly so much more useful than college was...

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

    Why does 8 remain on the left side of the 7?

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

      She's too busy copy and pasting her "BOOK" in different languages 🤣

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

    Why so many dislikes? That was the best you can learn of quicksort in under 10 mins.

  • @Developer-s6f
    @Developer-s6f หลายเดือนก่อน

    On the first iteration 8 > 7 so 8 should be moved to the right of 7. I think it is important to explain very carefully otherwise people will not get the idea or get confused :)

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

    how does it work when she is partitioning
    partition(low, index - 1)
    partition(index, high)
    shouldnt it be
    partition(low, index - 1)
    partition(index + 1, high)

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

    I implemented this in C++ with vectors and it worked.

  • @MajorBreakfast
    @MajorBreakfast 7 ปีที่แล้ว +24

    Oh man. This code looks fishy. The loop conditions inside partition() seem dubious to me. I doubt that this code was tested and reviewed thoroughly. You should throw some randomized testing and some eyeballs at your code to shake out all the bugs before you publish a video about it.

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

      Yeap, when I walk through the logic, it did not make sense to me and I just tried it with c#, it does not work.

    • @Manu-wb2uv
      @Manu-wb2uv 5 ปีที่แล้ว +1

      Code and logic works fine. I recommend to learn merge sort first

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

    i don't understand how this random floating pivot could possibly sort the list faster with how many iterations it looks like it takes to sort things in the correct order.

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

    why are we swapping left and right number if left is equal to right?

    • @LauraHumphreys
      @LauraHumphreys 10 หลายเดือนก่อน +1

      blondey tool is on indefinite vacation to answer anything

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

    if you first find the exact median/index item, then you can always median bucket sort to get O(n log n) performance

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

    on line 32, if they're equal then what's the point of swapping them? if they're equal swap does nothing, right?

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

    You didn't even sort the array fully. disappointed.

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

    Is it not useful to exchange the pivot with an up element in the end of partition function ? I don't see it in this implementation.

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

    Most of the tutorials are great but this is flawed and this implementation will not work for [5, 4, 1, 3, 7]. The pivot will be 1 and it is the smallest element in the array so the while loop that looks for something smaller than the pivot will go out of bounds.

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

    This implementation only works when pivot = left + (right - left) // 2 (Doesn't work on pivot = right). However, a more sufficient quicksort implementation should comply for any potential pivot point, that's the point for using quicksort

  • @RcVideos12
    @RcVideos12 6 ปีที่แล้ว +9

    she is comparing left and right, and swapping based on that, but those are indexes. They should be array[left] and array[right]

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

      @George Dedaj But the while loop is already doing that

  • @souravroy-ei3iq
    @souravroy-ei3iq 5 ปีที่แล้ว

    not working for {10,80,30,90,40,50,70}

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

    8 is greater than 7 but you put it on the left of 7 , really? will you pass Google' coding interview with this ?

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

    Hilariously this would fail the Quicksort part 1 and part 2 exercises on HackerRank because it doesn't maintain the order of the elements in each partition.

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

    Javascript implementation:
    let arr = [6,3,1,2,5,8,7,9,15];
    quickSort(arr);
    console.log(arr);
    function quickSort(arr){
    let left = 0;
    let right = arr.length-1;
    return quickSortHelper(arr,left,right);
    }
    function quickSortHelper(arr,left,right) {
    if (left >= right) {
    // console.log(arr);
    return arr;
    }
    let pivot = right;
    let index = partition(arr, left, right, pivot);
    quickSortHelper(arr, left, index - 1);
    quickSortHelper(arr, index, right);
    }
    function partition(arr,left,right,pivot){
    while(left arr[pivot]){
    right--;
    }
    if(left >= right){
    return left;
    }else{
    swap(arr,left,right);
    left++;
    right--;
    }
    }
    return left;
    }
    function swap(arr,left,right) {
    let temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;
    }

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

      Finalllyyyy! Thanks man, a solution in JavaScript that actually works

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

    Algo fails with ArrayIndexOutOfBoundException in few cases. try {4,1,3,6,5,8,7}

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

    3:23 When there is a sequence of number 213 and you make 1 as pivot, it is never sorted! coz 2 and 3 should both appear on the right side and they can not be swaped !!

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

    can anyone explain why index and index + 1 instead of the above index-1 and index would give me stack overflow?

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

    Wouldn't this break if pivot happens to be the largest or smallest in the list?

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

    In line 16, we have
    int pivot= array[(left+right)/2];
    and then in lines 28 and 32, we compare array elements at left and right indices with pivot.
    If I use int pivot= (left+right)/2;
    and then compare array elements at left and right indices with array[pivot], the sorting gets messed. I want to understand why. Any help would be appreciated.

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

      @@jasonc2033 I get that. My question is, if I take middle index as pivot, and then compare array[pivot] with elements in the left and the right, why does the answer vary. The sort messes up in this case.

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

    if the result is bigger than pivot one one side, why 8 is before 7?????

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

      blondey's too busy copy and pasting

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

    Will it work if array is having one or repeated number ?

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

    Anyone know why she has while loops moving indexes left and right if less than pivot without swapping?

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

    According to the code, the pivot can end up getting swapped, unlike in the animation at the beginning where the pivot was skipped. Am I seeing this correctly?

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

      You are correct, the code does not match the animation or her verbal description. The left scan stops at elements >= pivot, and the right scan stops at elements

  • @OmarGonzalez-tg9uv
    @OmarGonzalez-tg9uv 6 ปีที่แล้ว

    if left == right then there is no point in calling swap, it's the same element. This plus the index change make sure your code won't work properly.

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

    Thanks for the show off of your knowledge but I understood nothing. Seems I am not alone with that problem.

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

    This is In response to the older comments below about variations in quicksort. The partition algorithm shown in this video doesn't move the pivot value, but does swap elements so that all elements less than pivot are located before all elements greater than pivot, and elements equal to pivot are not swapped. Other partition algorithms, such as Hoare partition scheme, will end up swapping the pivot element to it's proper sorted location, swap all elements less than pivot before the pivot, swap all elements greater than pivot after the pivot, while elements equal to pivot may or may not be swapped, and can end up before or after the pivot.

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

    Hi Gayle, liking your book CTCI!

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

    I wish she did the quicksort on the right side instead of the left @ 2:00
    Once pivot is on the 7, the left pointer is stuck on the 8. Looks like her strategy would have the new partitions be [8][7,9,15]

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

    how did you move 8 before 7 ? It should have elements lesser than 7.

  • @mukeshkumar-lk2gl
    @mukeshkumar-lk2gl 6 ปีที่แล้ว

    Lets say i have array 8,7,9,15 and pivot is 9 then how it will work ?

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

    The code is working but in the video the swap method is not represented, because the swap method is too common for other sorting techniques. For the people who don't know what to put in the swap method, or for the people who don't know Java programming language, here's the additional code to insert in your class:
    public static void swap(int[] array, int left, int right) {
    int temp = array[left];
    array[left] = array[right];
    array[right] = temp;
    }
    public static void main(String[] args) {
    //Unsorted array
    int[] array = {3, 1, 8, 5, 6, 2, 4, 9, 7};
    //Sorting the array through static method quicksort
    Solution.quicksort(array);
    //Printing the numbers in sorted order one by one
    for(int i = 0; i < array.length; i++) {
    System.out.println(array[i]);
    }
    }

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

    so bubblesort.. ?

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

    Does the pivot ever get moved during a call to quick-sort?

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

    i did your code verbatim and it never reaches the base case

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

    Explanation in the beginning and the code at the end doesn't match. The while-loop in partition function (8:13), "right" stops decrementing when the value arrives at the pivot since pivot isn't greater than pivot itself. But in the explanation (1:44), your "right" index already passed pivot. You should change those while loops to if-statements and make it stop at array length or 0, and have swap in a better conditioned if-statement. Honestly, just rewrite partition function. Don't use nested while loops when you don't have to. It just creates confusion thinking squared time, when yours is in linear time. Concept-wise, good, but make sure you post an optimal programming solutions.

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

    @ 1:52 -- 8 > 7 but still 8 is on the lest side of 7, why?

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

      idiotBlondie's tool is too busy to care 🤣

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

    Try example {1,2,3,2}, and you will find the pi for the first partition is wrong, although the last result is correct.
    Let's go through it:
    1st partition:
    pivot=2
    * Initially: left = 0; right = 3;
    * After 1st loop: left = 2, right = 2
    * After 2nd loop: left = 2, right = 1
    * Exit from 3rd loop condition check
    * Return 2
    But actually it should return 1. This implementation seems have bugs in it.

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

    Learned many new concepts from this video. Today i got to know that 8 is smaller than 7 (:

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

    nice vid
    + another way to implement quicksort

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

    There is no hard & fast rule to select pivot, This code is not working when some choose last elements as pivot.

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

      yes, when arr[left] is bigger than the pivot, and choose last element as pivot, ends up both arr[left] and arr[right] are bigger than the pivot, so this will not work, cant swap, other wise will loop forever, like my program did.

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

    *You in your book, just like recruiters and interviewers at FaceBook say to go to **LeetCode.com** and your book to practice the interview questions. Also both admit that these are fundamental questions that everyone has seen before.*
    *You then carry on, and in your book say that if we have seen an interview question before, we should let the interviewer know, so, what is up with that? They ask questions that everyone has seen, and then we are supposed to let them know?*
    *They ask us to practice these questions, and then we are supposed to tell them that we have seen them before?*
    *Also you say tech companies are OK with false negatives, but what about false positives? people who read your book and pass the interview, but are not good!!!*

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

    You didn't bother explaining what happens to the pivot values after the partitions are sorted.... and that is literally the most important part :/
    They are supposed to swap into the spot where the left and right pointers overlap. Doing so guaranties that is their final sorted location.

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

    Hello, just to say regardless of the video. There is a Korean coding interview book right next to Apple computer.

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

    Wow, some insane comments. I like this implementation, although the upfront explanation was challenging to follow. Before someone comments that she has copied and pasted the solution, no kidding, since the original solution was invented by Tony Hoare in 1959, I couldn't find his TH-cam channel, unfortunately.

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

    while (array[right] > pivot && left < right) {
    ...}

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

    Shouldn't we be using Theta to showcase the average runtime rather than Big O?
    So, it should be:
    - θ(n log n)
    - O(n^2)
    no?

    • @bela-chan
      @bela-chan 5 ปีที่แล้ว +1

      There are cases where you have to perform n^2 operations, as the example showed in the video, so θ(n log n) does not bound the algorithm from above.
      The Theta notation does not mean the average runtime complexity. We use θ( f(n) ) when we want to indicate that the asymptotic complexity is bounded from above and below by c*f(n) for some constant c. Since that is not always the case, we don't have θ(n log n).

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

      @@bela-chan Thanks for clarification!

    • @bela-chan
      @bela-chan 5 ปีที่แล้ว +1

      @@riccardopolacci6501 No problem, hope it helped!