Algorithms: Quicksort

Поделиться
HTML-код
  • Опубликовано: 10 июл 2024
  • Learn the basics of quicksort. This video is a part of HackerRank's Cracking The Coding Interview Tutorial with Gayle Laakmann McDowell.
    www.hackerrank.com/domains/tut...
  • НаукаНаука

Комментарии • 502

  • @cheyno237
    @cheyno237 6 лет назад +2251

    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 5 лет назад +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 5 лет назад +7

      This is the method that I was taught at Uni.

    • @malecrium8665
      @malecrium8665 5 лет назад +15

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

  • @jimmyryan5880
    @jimmyryan5880 5 лет назад +254

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

    • @hujake5406
      @hujake5406 4 года назад +22

      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 лет назад +84

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

  • @emilybjoerk
    @emilybjoerk 7 лет назад +452

    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 6 лет назад +11

      this is a big plus in the interview

    • @barisyakut7970
      @barisyakut7970 6 лет назад +7

      Very good point ma'am.

    • @jaxonhundertwasser3068
      @jaxonhundertwasser3068 6 лет назад +2

      A much appreciated tip!

  • @rkcst6503
    @rkcst6503 7 лет назад +742

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

    • @doctor3397
      @doctor3397 7 лет назад +80

      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

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

  • @7heLostAndDamned
    @7heLostAndDamned 3 года назад +49

    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.

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

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

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

  • @florencechan8863
    @florencechan8863 4 года назад +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.

  • @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!

  • @HillWouston
    @HillWouston 6 лет назад +7

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

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

  • @VatsalRajyaguru17143
    @VatsalRajyaguru17143 3 года назад +19

    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 2 года назад +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.

  • @FalconAlliance1337
    @FalconAlliance1337 3 года назад

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

  • @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 3 года назад

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

  • @mandolinic
    @mandolinic 5 лет назад

    Great explanation. And the code works, too.

  • @ciphertester1147
    @ciphertester1147 5 лет назад +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?

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

  • @flatbill2
    @flatbill2 6 лет назад

    Nice and short videos. Really good for reviewing.

  • @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]);
    }
    }

  • @stas4985
    @stas4985 4 года назад +1

    nice vid
    + another way to implement quicksort

  • @shawnarmstrong3339
    @shawnarmstrong3339 3 года назад

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

  • @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--;
    }
    }

  • @KunalMukherjee3701
    @KunalMukherjee3701 3 года назад

    Now it makes sense finally!!!

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

  • @codinggyan6702
    @codinggyan6702 7 лет назад +1

    awesome tutorials , thanks a ton

  • @barisyakut7970
    @barisyakut7970 6 лет назад +72

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

  • @timcook3410
    @timcook3410 7 лет назад +24

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

  • @flying-musk
    @flying-musk 4 года назад +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

  • @imperialda2627
    @imperialda2627 2 года назад +2

    Actually i got confused by (left and right) i mean which one is value and which one is index. There should have been some distinction for them but the explanation was really good. Though code was a bit confusing.

  • @yhy78979
    @yhy78979 4 года назад +4

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

  • @Manu-wb2uv
    @Manu-wb2uv 4 года назад

    This feels like merge's sort little annoying cousin. Haha. Thanks a lot for the explanation. Very clear and on point. I understood just watching it once.

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

  • @Nathsnirlgrdgg
    @Nathsnirlgrdgg 5 лет назад +5

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

  • @brecoldyls
    @brecoldyls 6 лет назад +1

    Thank you, this helped me out so much!
    Also I can't be the only one to find it funny how the numbers and colorful letters fly around the screen during your example haha

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

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

  • @zhengrui315
    @zhengrui315 5 лет назад +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 4 года назад

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

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

  • @jeonghosuh1182
    @jeonghosuh1182 7 лет назад

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

  • @jingchen8562
    @jingchen8562 6 лет назад +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.

  • @xxksjq
    @xxksjq 5 лет назад +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 .

  • @randeepsiddhu
    @randeepsiddhu 4 года назад

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

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

  • @iandaley2295
    @iandaley2295 4 года назад

    These videos are great

  • @mads7401
    @mads7401 3 года назад

    Thanks, great explanation! :)

  • @rakeshjha2262
    @rakeshjha2262 5 лет назад

    Thanks a lot Gayle, your explanation is crystal clear.

  • @RomanKnav
    @RomanKnav 2 года назад

    Hi Gayle, liking your book CTCI!

  • @lordcom1798
    @lordcom1798 4 года назад

    Thanks. That video helped alot.

  • @sagarsahu263
    @sagarsahu263 4 года назад +8

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

  • @SantinaLin424
    @SantinaLin424 7 лет назад +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. 你說中文嗎

  • @hidenverma
    @hidenverma 7 лет назад +15

    The code used to explain quicksort is not completely right.

  • @aaroldaaroldson708
    @aaroldaaroldson708 7 лет назад +7

    code is not working

  • @garrettguitar6583
    @garrettguitar6583 3 года назад +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.

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

  • @francisaiello6197
    @francisaiello6197 5 лет назад +1

    Hi Gayle - I just want to thank you for taking the time for these videos - you're visual style is extremely beneficial for me (visual learner). I am grateful...

  • @OmarGonzalez-tg9uv
    @OmarGonzalez-tg9uv 5 лет назад

    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.

  • @chenthuransivanandan9874
    @chenthuransivanandan9874 4 года назад +1

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

  • @iWonderOfficial
    @iWonderOfficial 7 лет назад

    What a great channel! So glad I found this!

  • @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 7 месяцев назад

      She's just a dumb privileged Tool

  • @setYourHandleHttp404
    @setYourHandleHttp404 5 лет назад +1

    This explanation is quit different from the Quick Sort in the CORMEN book!

  • @jaypark6812
    @jaypark6812 6 лет назад

    It’s really helpful, I won’t tell you that at the first time I think this video is not good enough, what a fool.

  • @colinkanobe
    @colinkanobe 4 года назад

    Slowly unwinding this algorithm.
    Question all those while loops in the partition isn’t showing worst case time complexity of O(n^2) ?

    • @DanielNyong
      @DanielNyong 4 года назад

      yeah I think it's because they are not evaluated at the same time. Same as using an if else block. EDIT: I don't think that code works lol.

  • @F38000
    @F38000 7 лет назад

    Thank you so much Greets from Germany

  • @paddleyang4606
    @paddleyang4606 5 лет назад

    the best quick sort ever written is the princeton algs4 quicksort!

  • @modestocomputacao143
    @modestocomputacao143 3 года назад

    you produce the video lessons in which software?

  • @rcgldr
    @rcgldr 5 лет назад +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.

  • @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]

  • @nullbrain8552
    @nullbrain8552 4 года назад

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

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

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

  • @Mc-os1yc
    @Mc-os1yc 2 года назад

    While loop condition is very confusing. Say X is pivot, K is left element and Q is right element which means when you get to "IF" statement, it should satisfy this condition K > X and Q < X. If K is greater than X then K can never be smaller then Q because Q is smaller than K. It would only execute if K=Q.

  • @atalaguitare13
    @atalaguitare13 6 лет назад +1

    why if (left >= right) she just return and nothing to do ? We suppose to swap 15 and 6 no ?

    • @eigenmishiin3d47
      @eigenmishiin3d47 6 лет назад +2

      left and right aren't the values 15 and 6, they are the index numbers. the conditional statement is ensuring that the left and right pointers haven't crossed each other

  • @MicroUrb
    @MicroUrb 6 лет назад

    are all those imports necessary?

  • @helloapple2012
    @helloapple2012 5 лет назад +1

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

  • @CristianMihalyi
    @CristianMihalyi 7 лет назад +1

    It helped me a lot understanding better this algorithm

  • @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 !!

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

  • @unlockwithjsr
    @unlockwithjsr 4 года назад +1

    The best explanation so far, watched so many videos, and this one is the best, I guess becaus e of the visualizing part

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

  • @johnmistica396
    @johnmistica396 4 года назад

    Thank you!

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

  • @mergatafa8501
    @mergatafa8501 7 лет назад

    very good teacher

  • @hououinkyouma5372
    @hououinkyouma5372 3 года назад

    Thank you. Finally a thorough explanation :)

  • @rudram4534
    @rudram4534 2 года назад

    not ez to undesrtand but if understood you will feel that your brain is working

  • @MrOrcaCat
    @MrOrcaCat 5 лет назад

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

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

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

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

  • @abhilashpatel3036
    @abhilashpatel3036 5 лет назад

    Very good explanation mam

  • @CloudDinoGod
    @CloudDinoGod 7 лет назад +5

    7:08 shouldnt it be greater than or equal to the pivot, and less than OR EQUAL to the pivot

  • @jmancherjeee
    @jmancherjeee 5 лет назад

    I wonder how many times she's said "Gayle Laakmann McDowell, author of Cracking the Coding Interview" in her lifetime

  • @tomyang7788
    @tomyang7788 4 года назад

    array is not sorted what happens after the 2 indexes meet at the middle ? It's 12 3 4 65 9. How does the 6 move after the 5 then

  • @lijunyin1341
    @lijunyin1341 6 лет назад +2

    this video makes it more confusing to understand, at least for me. "Quick Sort - Computerphile" is much better video for me

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

  • @mayankgupta9742
    @mayankgupta9742 3 года назад

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

  • @andreroodt4647
    @andreroodt4647 3 месяца назад

    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 RUclips channel, unfortunately.

  • @aqibos
    @aqibos 7 лет назад +69

    OK... going back to Derek Banas! Lol.