Quick Sort For Beginners | Strivers A2Z DSA Course

Sdílet
Vložit
  • čas přidán 21. 07. 2024
  • Notes:
    Problem Link: bit.ly/41sTzt8
    Full Course: bit.ly/tufA2ZYt
    Notes/C++/Java/Python Codes: takeuforward.org/data-structu...
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 - intro
    1:10 - Flow Chart
    3:28 - Explanation of the Algo
    19:29 - Psuedo Code & Dry run
    27:32 - Note about comparator signs
    29:01 - Important Point
    29:52 - Online code
    32:31 - Time Complexity and Space complexity
    34:22 - Outro

Komentáře • 389

  • @takeUforward
    @takeUforward  Před rokem +136

    Let's march ahead, and create an unmatchable DSA course! ❤
    The worst case complexity will be O(N ^ 2) if we end up choosing the largest or smallest element as the pivot always. We will add this in the notes in the description. I missed this in the video.

    • @yashhokte1020
      @yashhokte1020 Před rokem +9

      Yes striver , even i was thinking same that you didn't explain this thing , btw thankyou so much for this much crystal clear explaination ..

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

      Hi striver @takeUforward ,
      when are you going to release video solutions for string type problems and heaps?

    • @yaswanthmitta8983
      @yaswanthmitta8983 Před 11 měsíci +6

      There is a minor mistake in your algo at 23:54 in while loop condition must be arr[i]

    • @teen_python_go9947
      @teen_python_go9947 Před 3 měsíci +1

      Since, we are always choosing pivot to be the first element of the array, we can always avoid the O(n^2) case by pre-checking if the array is pre-sorted (with O(n) Time Complexity) and if it is not then only feed it into the quick sort function.

  • @yashsharma6112
    @yashsharma6112 Před 10 měsíci +93

    I have to state it that "I tried to learn all sorting techniques various time. I learned but after a few days I forgot. But when you just added the real meaning of each sorting techniques. Like why it is called as selection sort and so on.... SO now I just remember their meanings and write the algorithm on my own." Thank you very much. Loved your teaching style

  • @deepanshuthakur140
    @deepanshuthakur140 Před 9 měsíci +21

    I tried to learn this from every yt channel but striver is the one i got it from, respect++

  • @FunkyPanda6263
    @FunkyPanda6263 Před rokem +23

    1 video every 2 days...
    Seems TRUE ❣️

  • @adityamaheshwari4250
    @adityamaheshwari4250 Před rokem +17

    Really you make everything a cakewalk!
    Thank you so much sir, it takes a big heart to do such a lot for the community for free❤

  • @ParasSharma-mf8or
    @ParasSharma-mf8or Před rokem +45

    Thanks for this amazing lecture,this is my humble request please complete this course as soon as possible.

  • @Kaushik846
    @Kaushik846 Před 6 měsíci +14

    Probably one of the crisp and to the point explanation of quick sort algorithm available online!!

  • @AdityaSharma-hs8os
    @AdityaSharma-hs8os Před rokem +6

    please upload full course you are douing a good job bhaiyaaa ,you are really a honest teacher other youtubers who has million subscribers just make us fool on name of dsa course ,they just tell the problem and paste the soultion but you solve every aspect -f our doubt please cpmplete this and dont worry of views and watch time,time will come when everyone will know who is the best teacher on youtube for dsa

  • @parthmangalkar
    @parthmangalkar Před 8 měsíci +3

    Understood!
    Thank you!! You are the best!
    Thanks a lot for making this DSA playlist! It really is helping me a lot!

  • @isaacreyes7563
    @isaacreyes7563 Před 12 hodinami

    Damn, I have been looking at sort, recursion, etc forever. I was first confronted with merge/quicksort back in 2019. Been looking at them from various other sources over the years but nobody ever explained it like you do. You are absolutely amazing at this stuff. Idk where you are in life but I hope you go onto make amazing things because someone with this in depth knowledge shouldn't be stuck teaching!

  • @omkarsawant9267
    @omkarsawant9267 Před 7 měsíci +12

    Quick Sort's in-place partitioning makes it more memory-efficient than Merge Sort in practice, but the worst-case space complexity can be higher when the partitioning is unbalanced.
    Time Complexity:
    Best Case: O(n log n) when the pivot choices consistently lead to balanced partitions.
    Average Case: O(n log n)
    Worst Case: O(n^2) when the pivot choices consistently lead to unbalanced partitions. However, with good pivot selection strategies (e.g., using the median element), this can be mitigated.
    Space Complexity:
    O(log n) auxiliary space for the recursive call stack in the best and average cases.
    O(n) in the worst case when the partitioning is highly unbalanced.
    Quick Sort's in-place partitioning makes it more memory-efficient than Merge Sort in practice, but the worst-case space complexity can be higher when the partitioning is unbalanced.
    Quick Sort tends to perform well in practice and is often faster than other O(n log n) algorithms, but its worst-case time complexity is worse than Merge Sort.
    Merge Sort's space complexity makes it less memory-efficient compared to some other sorting algorithms, but its stable performance and guaranteed O(n log n) time complexity in all cases make it a preferred choice for certain scenarios.
    Space Complexity:
    O(n) additional space is required for the temporary arrays during the merging process.
    It has a space complexity of O(n) due to the need for additional space for merging.

    • @Jinntechive
      @Jinntechive Před 3 měsíci +1

      Appreciate the effort you put for writing this comment 😇❤

    • @nayankhuman1043
      @nayankhuman1043 Před měsícem +1

      Thanks for the comment. i was looking for it.

  • @pranshuatrey
    @pranshuatrey Před rokem +4

    This is gold... Plz keep doing this..

  • @keshariaman
    @keshariaman Před rokem +5

    Thanks a lot for Quick Short. Feels easier to understand 🥰

  • @CodeBoost8375
    @CodeBoost8375 Před rokem +4

    Thanks for recursive effort brother.And till now all your lectures are absolutely awesome 🔥🔥

  • @mityamkumar1158
    @mityamkumar1158 Před rokem +3

    Understood.... 💯💯 Excited for Arrays playlist❤

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

    striver you are the best out of best....this tutorial is just amazing and you are like god for us A big thank you so much for your effort 🙂

  • @ShivamGupta-xw6gh
    @ShivamGupta-xw6gh Před rokem +1

    best explanation of quick sort on youtube

  • @makerNg1817
    @makerNg1817 Před 9 měsíci +2

    it's very understandable way you teach. thank you for this amazing lecture

  • @animeshmishra6280
    @animeshmishra6280 Před rokem +20

    how do you know which doubts are going to come in my mind. GREAT LECTURE SIR 🔥

    • @edisane8763
      @edisane8763 Před 9 měsíci +3

      Because once he was also in the same place as we are now and he worked hard to reach this point now he is helping us

  • @ahssanakhtar5746
    @ahssanakhtar5746 Před 2 měsíci

    Excellent content about DSA .I am follwing you A-Z coarse and improve my self in DSA day by DSA Thanks for making such a amazing content

  • @rajkumarvb5197
    @rajkumarvb5197 Před 11 měsíci +1

    Manhhhh 🥵, you are awesome. I can see the effort you are putting in! Thanks a lot! ❤

  • @ArunKumar-sk9jl
    @ArunKumar-sk9jl Před 9 dny

    A great man with the best of the best teaching skills and a kind attitude to make it free is awesome ❤

  • @alessandrocamilleri1239
    @alessandrocamilleri1239 Před rokem +7

    Excellent explanation as usual. Thank you.
    I am posting the iterative version which should further save on recursion call stack space. I have used a queue as the data structure but stack works just as well.
    void quickSort (vector &nums)
    {
    int n = nums.size();
    queue q;
    q.push({0, n-1});
    int low, high, pivot, i, j;
    while(!q.empty())
    {
    low = q.front().first;
    high = q.front().second;
    q.pop();
    if (low >= high) continue;
    pivot = nums[low];
    i = low; j = high;
    while (j > i)
    {
    while (nums[i] pivot && j > 0)
    j--;
    if (j >= i)
    swap(nums[i], nums[j]);
    }
    swap (nums[low], nums[j]);
    q.push({low, j-1});
    q.push({j+1, high});
    }
    }

    • @balakrishnanr648
      @balakrishnanr648 Před rokem

      So Yah u tried the iterative version of Quick Sort but still you are using the space what the Recursive func calls use in func call stack in the QUEUE FORM. O(logN) Queue takes as extra space.
      Like the point is Quick sort no matter what -> You can further optimize. Func Stk Space or in Iterative normal stack or queue space is needed.
      As we need to store it somewhere -> what are the next range of places where it is unsorted.
      Either use the system's func call stack or make ur own.

  • @meshkatuddinahammed
    @meshkatuddinahammed Před rokem

    Can't thank you more. Great lectures. Appreciate it.

  • @darkelixir2517
    @darkelixir2517 Před rokem +1

    Understood, will be a quick way to remember the algorithm, well taught!! Thanks

  • @tanmaykarmakar6224
    @tanmaykarmakar6224 Před rokem +22

    Quick sort in Descending order-(PYTHON)
    arr=[25,1,8,7,32,2,5]
    def piviot(arr,high,low):
    piviot=arr[high]
    i=high
    j=low
    while(i=piviot and i

    • @sumitapathak2900
      @sumitapathak2900 Před 8 měsíci +4

      Hey I haven't done a dryrun of your code but as i has index of high how can in increment in the while loop?

    • @jayaveersai1446
      @jayaveersai1446 Před 7 měsíci +1

      yes make sense and have to chnage the while (i

  • @cinime
    @cinime Před rokem +1

    Understood! Super amazing explanation as always, thank you very much!!

  • @gurudev8543
    @gurudev8543 Před rokem +3

    Best explanation ever ❤️❤️❤️
    Thanks bhaiya

  • @scoc55vora15
    @scoc55vora15 Před rokem

    Best , Detailed and Crisp

  • @SwatiSingh-ys6hm
    @SwatiSingh-ys6hm Před 10 měsíci

    Damn it, i have learnt sorting algorithms a lot of times, but i aways manage to somehow forget.But after seeing this video now i understand it in so much more detail and depth which i earlier didn't even notice. thanks you soo much striver !

  • @tusharkhandave374
    @tusharkhandave374 Před 9 měsíci

    Thanks for giving this content for free it helps me a lot

  • @abdulwahabshaik1686
    @abdulwahabshaik1686 Před rokem

    Understanding everything u r teaching to us u r magician striver

  • @Manoj_IIC-Admin
    @Manoj_IIC-Admin Před rokem +9

    at time 23:52 it should be pivot not ar[pivot] thanks bhaiya

  • @soumelee5661
    @soumelee5661 Před rokem

    Awesome explanation! TYSM for the videos

  • @AniketSingh-gm9nh
    @AniketSingh-gm9nh Před 6 dny

    u r just amazing. keep educating man u r blessing for us.❤

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

    //for descending
    while (arr[i]>=pivot && i

  • @rohanraj2604
    @rohanraj2604 Před rokem

    Crystal clear bhaiya 😍

  • @Manishgupta200
    @Manishgupta200 Před rokem +7

    Here is my Assignment question solution :
    #include
    using namespace std;
    int partition(vector &arr, int low, int high){
    int pivot = arr[low];
    int i = low;
    int j = high;
    while(i < j){
    while(arr[i] >= pivot && i = low + 1) j--;
    if(i < j) swap(arr[i], arr[j]);
    }
    swap(arr[low], arr[j]);
    return j;
    }
    void qs(vector& arr, int low, int high){
    if(low < high){
    int pIndex = partition(arr, low, high);
    qs(arr, low, pIndex - 1);
    qs(arr, pIndex + 1, high);
    }
    }
    int main(void){
    // vector v = {4, 3, 2, 1};
    vector v = {4, 3, 2, 1, 4, 7, 5, 6};
    int n = v.size();
    qs(v, 0, n-1);
    for(auto it : v) cout

  • @subhadeepghosh2813
    @subhadeepghosh2813 Před rokem

    Kaha se sikha hai aise padana?
    vai koi vi nahi hai tumhare jaisa.
    Maza aa gaya

  • @pradeep6635
    @pradeep6635 Před 2 měsíci

    understood Bhayya.The best explanation in youtube😎

  • @hareshnayak7302
    @hareshnayak7302 Před 3 měsíci

    Understood,Thanks striver for this amazing video.

  • @rayyanrahim7413
    @rayyanrahim7413 Před rokem +2

    love from pakistan we need these type of legend to teach progamming

  • @AbdullahKhan-et6qo
    @AbdullahKhan-et6qo Před rokem

    Was eagerly waiting for your videos 🙌

  • @lakshmitirupatamma2139

    Thank you so much sir for this content. Very good explanation

  • @rishabhkumar-qs3jb
    @rishabhkumar-qs3jb Před 2 měsíci +1

    Excellent explanation :)

  • @yhbarve
    @yhbarve Před rokem +1

    Understood bhaiya! Thank you

  • @sumit49
    @sumit49 Před rokem

    us!!
    please add it in the same playlist as that will be more organised .
    ❣❣

  • @bonammadankumar681
    @bonammadankumar681 Před 4 měsíci

    You rocks the DSA

  • @manavsingh5919
    @manavsingh5919 Před rokem

    Understood sir.....u r the best👍💞

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

    Wonderful Explanation!!
    Notes include the space complexity as O(1)+O(N) auxiliary stack space. Is it the worst case space complexity? And is the best and average case auxiliary space complexity O(logN)?

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

    Instead of 3,2,1 it should be 1,3,2 coz we have done swapping b/w 4 & 1 like swap(arr[low], arr[j]) right ?

  • @DevashishJose
    @DevashishJose Před rokem

    Understood. Thank you so much.

  • @revathiP9185
    @revathiP9185 Před rokem

    Thank you bhaiya. Amazing explanation ❤

  • @THE-MYSTIC
    @THE-MYSTIC Před rokem +1

    Thanks striver this video helped me ❤

  • @Hipfire786
    @Hipfire786 Před 3 měsíci

    understood everything sir so far all sorting techniques

  • @basusingg4882
    @basusingg4882 Před 7 měsíci

    Very well understood!

  • @ankit-yj7hk
    @ankit-yj7hk Před měsícem

    understand bhaiya !!!
    thank uh so much

  • @gulammohammadali4831
    @gulammohammadali4831 Před rokem

    Very well understood 🙂

  • @poojaraman6663
    @poojaraman6663 Před 26 dny

    Understood...thank u sir🙂

  • @mokshmehan7695
    @mokshmehan7695 Před 10 dny

    Great work

  • @sakshamtyagi2969
    @sakshamtyagi2969 Před 5 měsíci

    Completely Understood 👍👍

  • @saranshgautam3011
    @saranshgautam3011 Před 9 měsíci +2

    Why do we ignore recursion stack space when calculating Space Complexity? I think that should count.

  • @yashpandey7
    @yashpandey7 Před měsícem

    understood , Step 2 completed :)

  • @thePRECIOUS_1
    @thePRECIOUS_1 Před 8 dny

    amazing content.

  • @____Akash__
    @____Akash__ Před rokem

    After long time ❤️❤️

  • @techniciangurugaming5020
    @techniciangurugaming5020 Před měsícem

    UNDERSTOOS VERY WELL

  • @iamnoob7593
    @iamnoob7593 Před 6 dny

    Just Superb

  • @lwnflash2123
    @lwnflash2123 Před 3 měsíci +2

    Is recursive stack space not required while computing space somplexity?

  • @shreyashkumarsahu5985
    @shreyashkumarsahu5985 Před 11 dny

    understood
    Thanks a lot sir

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

    Its a great video, but you should also explain the cases where complexity for quick sort can result to O(n^2) in the case where all elements of array are same and when array is already sorted, in those cases partition will always be 1, n-1.

    • @krishnendughosh2368
      @krishnendughosh2368 Před 7 měsíci

      sorted or even if sorted in descending order complexity will be n^2

  • @tasneemayham974
    @tasneemayham974 Před rokem +10

    CODE FOR DESCENDING (JAVA):
    public void quickSort(int[] arr, int low, int high){
    if(low low){
    j--;
    }
    if(i

    • @DineshKumar-pw7qb
      @DineshKumar-pw7qb Před 10 měsíci

      When I try to run this code(using array), I getting no output and the code runs for infinite time. When I try with ArrayList, I am getting output.
      Explain why? This put me into severe headache

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

      @@DineshKumar-pw7qb Heyy!! You mean my code?
      Either way, can you send me the code you are working on. I want to try it. How about that? Maybe we can fix it together?

    • @DineshKumar-pw7qb
      @DineshKumar-pw7qb Před 10 měsíci

      @@tasneemayham974bro are sure about your code is working well with array?

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

      @@DineshKumar-pw7qb Hey, mate! I copied my code, and yes it works with arrays. It doesn't give me errors or infinite loops, but...
      this line:
      while(arr[i] > pivot && i < high)
      I missed the =. I am so sorry. I should be:
      while(arr[i] >= pivot && i < high)
      Check it out now. But as I understand, the problem with your code is the array itself, yes? Not the output? If you want you can send the code. Because mine works well with array.
      P.S.: I will edit the original comment, and put the equal.

  • @JothiprakashThangaraj
    @JothiprakashThangaraj Před 58 minutami

    Understood!! Thanks!!!

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před rokem +1

    Good explanation us

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

    For Descennding Order Sorting ->
    //Just Reverse the inequality sign in partition function :-
    #include
    int partition(vector&arr,int low,int high){
    int pivot =arr[low];
    int i=low ,j=high;
    while(i

  • @mansiyelgulwar4765
    @mansiyelgulwar4765 Před rokem

    Understood
    Thank You!!!

  • @ankiitttkmmm
    @ankiitttkmmm Před rokem +1

    man you awesome Thanks

  • @NazeerBashaShaik
    @NazeerBashaShaik Před 4 měsíci

    Understood, thank you.

  • @_hulk748
    @_hulk748 Před rokem

    Thank you sir🙇‍♂️🙏❤

  • @ddevarapaga5134
    @ddevarapaga5134 Před 27 dny

    You are the best

  • @aakarshdhingra6763
    @aakarshdhingra6763 Před 26 dny

    UNDERSTOOD!

  • @konankikeerthi
    @konankikeerthi Před měsícem

    Thanks bro.. understood

  • @ankush8243
    @ankush8243 Před 7 měsíci

    Thank you bhaiya!

  • @arnavanant3049
    @arnavanant3049 Před rokem +1

    you are OP bhaiya

  • @zyzzbrah9429
    @zyzzbrah9429 Před měsícem +1

    int partition (int arr[], int low, int high)
    {
    int p=arr[low];
    int i=low,j=high;
    while(i=p && i

    • @shailesh3348
      @shailesh3348 Před měsícem

      I also completed 2 steps can we connect?

  • @harshitjaiswal9439
    @harshitjaiswal9439 Před rokem

    Amazing!

  • @user-ut2te7td7e
    @user-ut2te7td7e Před 11 měsíci

    After checking all lectures on internet...None has explained better than you

  • @makingthamilan1592
    @makingthamilan1592 Před 7 měsíci

    you are my God , thanks man 🥰

  • @bhagyashreekhairnar683
    @bhagyashreekhairnar683 Před 5 měsíci

    Thank you thank you !!

  • @AbhishekKumar-cd4gg
    @AbhishekKumar-cd4gg Před 11 měsíci

    // for descending order
    just we have to do the tweaks in the how we are selecting elements when which we are stopping when we are finding element smaller in left and stop when we find the element greater the pivot then we just we swap it
    than it goes in the recursion stack
    public class Quicksort {
    static int partiton(int arr [] , int low , int high ){
    int pivot = arr[low];
    int i= low;
    int j = high;
    while (i < j ) {
    while (arr[i] >= pivot && i = low + 1 ) {
    j--;
    }
    if(i < j ){
    int temp = arr[i];
    arr[i] = arr [j];
    arr[j] = temp;
    }
    }
    int temp = arr[j];
    arr[j] = arr[low];
    arr[low] = temp;
    return j ;
    }
    static void quicksort(int arr [] , int low , int high){
    if(low < high ){
    int PartionIndex = partiton(arr,low,high) ;

    quicksort(arr, low , PartionIndex-1);
    quicksort(arr, PartionIndex+1 , high);
    }

    }
    public static void main(String[] args) {
    int arr [] = {10, 80, 30, 90, 40};
    int n = arr.length-1;
    quicksort(arr, 0, n);
    for (int i : arr) {
    System.out.print(i + " ");
    }
    }
    }

  • @nayankhuman1043
    @nayankhuman1043 Před měsícem

    Understood ! ❤

  • @piyushmanikantm
    @piyushmanikantm Před rokem

    why do we need to check for i

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

    Hey Striver Can you tell me which text editor you are using on the iPad?

  • @The_Shubham_Soni
    @The_Shubham_Soni Před měsícem

    Best Teacher

  • @edwarddk9007
    @edwarddk9007 Před 3 měsíci

    Hello, excellent explanation, here I made another version, but could you tell me if the version of the quick sort algorithm that I implemented is correct? function quickSort(array, min, max) {
    if(min < max) {

    const q = partition(array, min, max)
    quickSort(array, min, q-1)
    quickSort(array, q+1, max)
    }
    return array
    }
    function partition(array, min, max) {
    let q = min
    let j = min

    for(let i = min; i

  • @Kirito045
    @Kirito045 Před 9 měsíci

    Understood
    Striver on TOP!

  • @sid17_cgdyt
    @sid17_cgdyt Před rokem +1

    // condition i

  • @simranbandhu9926
    @simranbandhu9926 Před rokem

    why it's giving wrong output for swapping this as
    swap(arr[j], pivot) ; before return statement

  • @ranjanworld5450
    @ranjanworld5450 Před rokem

    I will watch the next video now

  • @YourCodeVerse
    @YourCodeVerse Před 9 měsíci

    Understood✅🔥🔥

  • @elizabethr5161
    @elizabethr5161 Před rokem

    why do we swap pivot with j and not with i . can someone please explain?