No video

Quick Sort Using Recursion (Theory + Complexity + Code)

  Рет қаралды 167,591

Kunal Kushwaha

Kunal Kushwaha

Күн бұрын

In this video, we cover the quick sort algorithm. Including the theory, code implementation using recursion, space and time complexity analysis, along with comparison with merge sort. We also see how in-built sorting algorithms work.
Take part in the learning in public initiative! Share your learnings on LinkedIn and Twitter with #DSAwithKunal & don't forget to tag us!
👉 Resources
- Join Replit: join.replit.com/kunal-kushwaha
- Complete Java DSA playlist: • Java + DSA + Interview...
- Code, Assignments, & Notes: github.com/kunal-kushwaha/DSA...
➡️ Connect with me: kunalkushwaha.com
=========================================
Timestamps:
00:00 Introduction
03:01 What is Pivot?
04:43 Recursive Quick Sort
09:58 How to put pivot at correct position?
17:55 How to pick the pivot?
20:15 Complexity comparison of pivot positions
27:45 Important Notes
29:32 Hybrid Sorting Algorithms
31:49 Code for Quick Sort
39:12 Internal Sorting Algorithm
41:20 Outro
#sorting #placement #dsa #interviews

Пікірлер: 196
@KunalKushwaha
@KunalKushwaha 10 күн бұрын
DSA + interview preparation playlist: kzfaq.info/sun/PL9gnSGHSqcnr_DxHsP7AW9ftq0AtAyYqJ
@SochtaHoonMain
@SochtaHoonMain 10 ай бұрын
Kunal Kushwaha : "Your start is low , Your End is going to be high" :)
@hc-dc7043
@hc-dc7043 2 жыл бұрын
One of the best explanations I've seen for Quick Sort. The way he broke the concept down to its nuts and bolts is really amazing.
@zainabfahim8714
@zainabfahim8714 2 жыл бұрын
The confidence you have in your work, is empowering!! Keep that going
@sagarmore9425
@sagarmore9425 2 жыл бұрын
I have tried to understand merge sort and quick sort literally a thousand times. In college, classes, some time by me. I was not able to visualize how it is actually working with recursion, although I knew the code. But the way, you are simplifying the things and the way you have had the first recursion video done is just awesome. That first recursion video is quite important and on top of this you explained this sorting so well, kudos man!
@Sillysmiles76
@Sillysmiles76 Жыл бұрын
Such a blessing to learn DSA with your videos.😇 Please complete the series.🙂
@nishantsingh8477
@nishantsingh8477 2 жыл бұрын
There's this merge sort and quick sort, people say its hard to understand this and so I left them and proceeded further in my DS journey. But I was damn sure you will make it easy and so listened to you and Now I know this . Thankyou KK Bhaiya 🤩
@Tomharry910
@Tomharry910 Жыл бұрын
I remember plucking out my hair on recursion and sorting algos before stumbling on your videos. You explain it very well and its very intellectually satisfying to watch your videos as well.
@rohitchilhorkar8114
@rohitchilhorkar8114 2 жыл бұрын
I can't even express how much value it is providing us...hats off kunal bhai!
@mk_ahmar
@mk_ahmar Жыл бұрын
Hey Kunal, your lectures are valuably explained, and covers everything one should know sooner or later. For example after watching this QuickSort lecture, all I've to do is practice some question, I don't need to watch any more concept of this anywhere, and this is amazing. You and your video lectures are underrated, if this quality of lectures are provided by any EdTech they would have charged that course extravagant. Hope I'll be able to get ₹7Lakh/month 😂(Joking).
@KunalKushwaha
@KunalKushwaha Жыл бұрын
you wont get 7 lakhs per month with dsa, it happens with dev
@tejasvijain1077
@tejasvijain1077 2 жыл бұрын
Before watching this vid, i was on gfg and javapoint understanding what quick sort was and my god, kunal has explined it so well and he hasnt copied what they did and I was blown by how easy he has made this!! Kudos bro,
@karthikpm9669
@karthikpm9669 2 жыл бұрын
this is good...i mean its good that you didnt advertise your videos in a humble way , because people wont know how good your videos are. PS i didnt realise how fast time went , the way you taught was very smooth and seemed like a story to me.
@Helly_Patel
@Helly_Patel 2 жыл бұрын
Thanks for teaching us like no one did till now!! One small request, please make lectures on dynamic programming as well!!
@sravansunkara9145
@sravansunkara9145 Жыл бұрын
I feel very confident whenever I learn from you, Thank You for existing, God bless you!!!
@VinodKumar-fn2iq
@VinodKumar-fn2iq 2 жыл бұрын
Kunal is very quick now 🔥 Thanks for the course ❣️
@vidhijain2945
@vidhijain2945 2 жыл бұрын
The way you have explained is great Kunal!
@KunalKushwaha
@KunalKushwaha 2 жыл бұрын
Thank You!
@Cloud-577
@Cloud-577 2 жыл бұрын
thank you I actually like this implementation better than other ones I have seen. this will stick with me, no doubt
@shantahiremath7313
@shantahiremath7313 4 ай бұрын
Thank you Kunal Sir. You are always the best teacher. I never learned this sort by fearing the complexity. But now you made it a cakewalk. Always grateful.
@AshishKumar-ys7rj
@AshishKumar-ys7rj 8 ай бұрын
best lecture on yt for quick sort because it is the most efficient i have seen, but first watch the quick sort algorithm by striver, he will clear all the doubts if you have got any by watching this lecture , since i had lot of doubts . Once you understand the algorithm u can follow the code here . The concept will be crystal clear then .
@baap6382
@baap6382 2 жыл бұрын
Wow, you are so good. Wish you to solve complex algorithms down the lane
@vikaspanwar5194
@vikaspanwar5194 2 жыл бұрын
thanks for uploading at this frequency. Hope that you will cover dp really soon. 👏🙌
@requisits
@requisits Жыл бұрын
Thank you kunal finally understood the concept deeply.
@devanshgaur1747
@devanshgaur1747 2 жыл бұрын
Bhaiya ,aap sachii mein bohot achaa padhatee hoo. Thanks bhaiya for such a course
@vinaykrishna747
@vinaykrishna747 Жыл бұрын
Please Use Black Background for writing on the screen It doesn't affect eyes much 😊😊 Consider this Man
@talish7714
@talish7714 2 жыл бұрын
good job kunal and never gave up bro keep it up
@charaiveticharaiveti8414
@charaiveticharaiveti8414 2 жыл бұрын
Kunal, your tutorials are gems. I can pay for such quality content. But you choose not to. Love and Regards. Tweeting for better reach.
@civuasdev5532
@civuasdev5532 2 жыл бұрын
Hey kunal, Thanks for this course.
@anjalithakur_12
@anjalithakur_12 2 жыл бұрын
Very good explanation just loved it
@shivamrana4633
@shivamrana4633 5 ай бұрын
You are boosting my confidence day by day..🎉😊
@spdwivedi5925
@spdwivedi5925 18 күн бұрын
Great video kunal
@shikhamaurya4453
@shikhamaurya4453 2 жыл бұрын
Ohh damn, This was so well explained 45 minutes is totally worthy
@KunalKushwaha
@KunalKushwaha 2 жыл бұрын
Thank You!
@twi4458
@twi4458 2 жыл бұрын
Can anyone help me in understanding 24:50, How we got O(N^2). It is Linear Recurrence Relation, Right?
@ritviksaran6199
@ritviksaran6199 2 жыл бұрын
after each pass how does it ensure that pivot will end up in the correct position ?
@piyush9266
@piyush9266 2 жыл бұрын
U knw bro..i think u r inborn Guru not a professional teacher.may god bless u with all Happiness of this universe.🙏
@geetankarkarmakar378
@geetankarkarmakar378 2 жыл бұрын
Mad stuff ❤‍🔥❤‍🔥 Thanks a ton !!
@amruthaa5876
@amruthaa5876 2 жыл бұрын
Thank you so much, Kunal!
@vanshsharma2486
@vanshsharma2486 2 жыл бұрын
great video bro, thank you so much!!
@rakeshkumarmaurya8265
@rakeshkumarmaurya8265 2 жыл бұрын
Best DSA COURSE on EARTH 🔥🔥 THANKU @kunal kushwaha
@sravanakirtana2787
@sravanakirtana2787 4 ай бұрын
Awesome. You make everything very easy. (In your words easy peasy 😅)
@Mohit_Gupta24
@Mohit_Gupta24 3 ай бұрын
As always brilliant explanation. I used to be scared of quick sort in my college days, but this video makes it seem really easy. Moreover, I was able to relate Step: placing pivot at the correct index with Binary Search step. In binary search, we search for a target element, assuming the array is sorted, and use the start and end indexes as pointers to the virtual array. Here we have to just place the pivot at the correct index, so instead of searching the target, we already have the target(i.e. pivot), we have to put at correct index and in this process we are ensuring elements less than pivot are on LHS, and elements greater than pivot are RHS.
@yourmentor8242
@yourmentor8242 2 жыл бұрын
Hey kunal please bring some leetcode sorting questions problem videos,and yes Thank you so much for this Awesome content bro ❣️
@nishu0766
@nishu0766 5 күн бұрын
i request you to please upload all the remaining topics of this complete DSA bootcamp as soon as possible
@kirankumar9729
@kirankumar9729 2 жыл бұрын
Fabulous explanation..
@poojakatare1497
@poojakatare1497 4 ай бұрын
A very informative and interesting lecture 😎
@NilavraPathak
@NilavraPathak 2 жыл бұрын
I think quick select is also a closely related technique that can be used to solve many problems.
@VishalKumar-pk9ek
@VishalKumar-pk9ek 2 жыл бұрын
In the merge sort video , I was little confused in the concepts of sorting algorithm through recursion but by this video , the previous confusion also got cleared . Thank you so much 😍😍😍
@KunalKushwaha
@KunalKushwaha 2 жыл бұрын
You're very welcome!
@vikassk1308
@vikassk1308 2 жыл бұрын
@@KunalKushwaha little confused, when you say pivot would be in the correct position after swaps if I have below array and I choose 4 as pivot {8,6,1,4,2,12} after swaps, ill end up having {2,4,1,6,8,12} where 4 is at index 1 but it should be at index 2 right ?
@suyashkumar8840
@suyashkumar8840 2 жыл бұрын
Love you kunal bhai ♥️♥️
@randheerkumargautam6433
@randheerkumargautam6433 2 жыл бұрын
will you make video on dynamic programming as well?
@Dekh_rha_hai_vinod.
@Dekh_rha_hai_vinod. 2 жыл бұрын
Bro amazing hetric👌👌
@firdoshansari2757
@firdoshansari2757 2 жыл бұрын
Hey Kunal, what is the possibile time you take to complete dsa and start with Opps??
@anshulgoel1940
@anshulgoel1940 Жыл бұрын
An alternative approach is to swap the pivot with the last index before we start in each recursive call and then start comparing left and right. Once index cross over, we swap pivot with left pointer. Works out much cleaner at code level.
@pratheeeeeesh4839
@pratheeeeeesh4839 11 ай бұрын
The approach which you told above does that calculate the pivot using the same mid concept?
@anshulgoel1940
@anshulgoel1940 11 ай бұрын
@@pratheeeeeesh4839 No. Pivot can be picked in any way. It won't make any difference at complexity level. This approach I suggested just keeps the code clean. Hope it helps
@ashishgaikwad440
@ashishgaikwad440 2 жыл бұрын
god level explain ❤️
@nav7848
@nav7848 2 жыл бұрын
Love u kunal bhaiya❤❤
@ritviksaran6199
@ritviksaran6199 2 жыл бұрын
Kunal bro this is a doubt. After a pass of the outer while loop, how can we confirm that pivot will end in correct index? For the array {1, 4, 2, 5, 7, 6, 3} after one pass changes to {1, 4, 2, 3, 7, 6, 5}. 5 was the chosen pivot but it does not end in correct position. The whole algorithm is correct at the end it will give the sorted array, but for this version I think we cannot confirm that after each pass pivot will end up in correct index.
@harcharansingh1997
@harcharansingh1997 2 жыл бұрын
Yeah bro having same doubt
@ritviksaran6199
@ritviksaran6199 2 жыл бұрын
@@harcharansingh1997 This method ensures that in each pass the pivot ends up in the correct index public class Main { public static void main(String[] args) { int [] arr = {1, 4, 2, 5, 7, 6, 3}; quickSort(arr, 0, arr.length - 1); for(int i : arr){ System.out.println(i); } }public static void quickSort(int [] arr, int low, int high){ if(low >= high) return; int s = low; int e = high - 1; int pivot = arr[high]; while(s pivot){ e--; } if(s
@MohamedElsayed-pb3oj
@MohamedElsayed-pb3oj 2 жыл бұрын
The reason you are having doubt is because the algorithm is incorrect. Tried it manually and tried it with the debugger and it still gave the first pass answer. Meaning the algorithm doesn't put the pivot in its intended location.
@takenbysac
@takenbysac 2 жыл бұрын
@@MohamedElsayed-pb3oj exactly Elsayed , this algo is wrong, I tried with negative numbers as input and it failed . Tried with {2,5,3,0,8,22,9,-1,-3,-5} as input . @Kunal
@takenbysac
@takenbysac 2 жыл бұрын
@@ritviksaran6199 even this got failed @ritvik for input as {2, 5, 3, -3, -1, -22, 0, 8, 22, 9}
@smilemania5102
@smilemania5102 2 жыл бұрын
Awesooooooooooooome ❤️❤️
@mrrishiraj88
@mrrishiraj88 2 жыл бұрын
Great thanks for very useful content in quite comprehensive form.
@prikshit_sharma4071
@prikshit_sharma4071 2 жыл бұрын
I Never learnt java. Should I start this course or not?🙏🏻🙏🏻
@KunalKushwaha
@KunalKushwaha 2 жыл бұрын
Yes
@jyotiprakashnayak1123
@jyotiprakashnayak1123 2 жыл бұрын
best course till date in any platform
@shyamalm88
@shyamalm88 2 жыл бұрын
Kunal Facing too much problem with sliding window technique. with strings and other things, it will be very helpful if you can create a video on this with solving some problems, not only the maximum sum problems but other problems also some tips or tricks will be fine. Love your work. your content really helpful.
@adilkhan-ft7xv
@adilkhan-ft7xv 2 жыл бұрын
Hi kunal, can you tell how long it will take for dynamic programming lecture??? Btw we are very thankful for your great work
@charon71
@charon71 4 ай бұрын
best videos here!
@sourabhkothari7014
@sourabhkothari7014 2 жыл бұрын
Best explanation
@NavanshK
@NavanshK Жыл бұрын
@kunal Please continue this course...
@jk-sm6qr
@jk-sm6qr 10 ай бұрын
Thank you Kunal
@KartiKKaushiKYt
@KartiKKaushiKYt 2 жыл бұрын
My profssor seriously needs to learn from this dude, I was stressing about this so much, this titorial is elite. THANK YOU SO MUCH
@anukritisingh4479
@anukritisingh4479 Жыл бұрын
pls upload dp series soon
@924_aradhyapatro5
@924_aradhyapatro5 2 жыл бұрын
This is by far the simplest explaination of Quick Sort that i have found
@dineshmadduru7043
@dineshmadduru7043 2 ай бұрын
Thank you sir
@vigyanta94
@vigyanta94 10 ай бұрын
Thanks Kunal
@ayushmaheshwari809
@ayushmaheshwari809 10 ай бұрын
Hey Kunal , why pivot element not place correct index after one recursion call ? Although the array is sorted in the end .
@Videolyy
@Videolyy Жыл бұрын
loved it..
@suhaibalam1040
@suhaibalam1040 11 ай бұрын
Thanks a lot man
@harshaggarwal1821
@harshaggarwal1821 2 жыл бұрын
@36:05 you mentioned swapping doesn't happen if array already sorted. But taking {1,2,3,4,5} it does call swap with same indices.
@chennayaianjc3306
@chennayaianjc3306 2 жыл бұрын
I think he said , swap will not happen if it is already sorted .how? in while(s
@parmarkamlesh6493
@parmarkamlesh6493 10 ай бұрын
Hi Kunal, are you planning to launch any playlist?
@kishorevb8331
@kishorevb8331 2 күн бұрын
With the code you provided, it will not place the pivot at the right spot after one pass, but still it works!!! It doesn't place the pivot in the right index due you incrementing and decrementing the start and end after each swap. We must increment and decrement only when provided that the value in that index is lower than or higher than the pivot
@bishalbhattacharjee850
@bishalbhattacharjee850 2 жыл бұрын
Great teaching please make a video on heap sort as well...
@KunalKushwaha
@KunalKushwaha 2 жыл бұрын
Sure I will
@mihirjadhav5642
@mihirjadhav5642 2 жыл бұрын
why did you delete your old videos?? where do we find them now?
@rudram4534
@rudram4534 2 жыл бұрын
still watching consistently 😀😀 😀😀
@absingh773
@absingh773 2 ай бұрын
13:14 pivot at correct position❌every element at correct position✅
@frosted_soul_
@frosted_soul_ 2 жыл бұрын
ku ku ku kunal...kunal....kunal.....kunal.....
@travelnlearn
@travelnlearn Жыл бұрын
AMAZING
@PiyushMishra-tq6cl
@PiyushMishra-tq6cl 6 ай бұрын
Hey Kunal, How did you do the DSA at your time? Like did you rely on tutorial videos or some book?
@gitansh194
@gitansh194 2 ай бұрын
00:05 Quicksort is a crucial sorting algorithm frequently asked in interviews. 02:11 Quick sort uses pivot to divide elements into smaller and larger groups. 06:22 Quick Sort uses recursion to position the pivot element correctly after each pass. 08:20 Quick sort uses recursion to sort arrays efficiently 12:24 Understanding how to identify violations while sorting using Quick Sort. 14:35 Moving the pivot to the correct position using recursion 18:43 Picking the pivot element in quick sort 20:34 Understanding time complexity and recurrence relation in Quick Sort. 24:23 Quick sort's time complexity is O(n^2), covered in detail 26:22 Recurrence relation of merge sort explained with time complexity formulas. 29:36 Hybrid sorting algorithms combine merge sort and insertion sort for efficiency. 31:26 Exploring new approaches to solve problems is recommended. 34:59 Moving elements according to pivot and violating conditions 36:36 Explanation of the swapping process and recursion call in Quick Sort using recursion. 40:23 Quick sort is a hybrid sorting algorithm 41:58 Quick Sort using recursion is a common sorting algorithm.
@jacksongeorge4047
@jacksongeorge4047 2 жыл бұрын
hey can you make a video about how to apply remote jobs ...
@GunjanFitness
@GunjanFitness Жыл бұрын
Can you please continue this course and complete it soon
@gowthamselvaraj7793
@gowthamselvaraj7793 10 ай бұрын
Do a separate series for Dynamic programming bro..
@satnamsingh3801
@satnamsingh3801 Жыл бұрын
Fabulous
@immortalji
@immortalji 2 жыл бұрын
Hey bhaiya, How to stay away from -ve people? and stay focused toward own goal(SDE OR DATA SCIENTIST).
@prashantmishra9220
@prashantmishra9220 2 жыл бұрын
@kunalKushwaha Bhaiya aapne to book follow krke pdhai ki hogi....to hmlog sirf is playlist ko follow krenge kya sach me utna hi knowledge gain kr paenge...kya is playlist me jitna pdhaya gya hai utna hi pdhne se sach me acchi job crack kr paenge? pls reply
@prashantgoswami1491
@prashantgoswami1491 Жыл бұрын
Kunal i am waiting for some other topic vedio please kindly upload it I am going to have my placement in next sem
@itsom5294
@itsom5294 2 жыл бұрын
Hey kunal bring some leetcode maximum subarray type questions problem videos
@KunalKushwaha
@KunalKushwaha 2 жыл бұрын
I will
@karanbadhwar4113
@karanbadhwar4113 3 ай бұрын
The problem is I did not understand the Time and space Complexity After recurrence relation, as I am do not understand the Maths that you did, it took me more than a week to any how complete the video, but now my question is how can I do that, as I did not understand them Maths you taught, any other way to solve it ?
@sangamsaini138
@sangamsaini138 2 жыл бұрын
bhaiya if conditon lagana ka koi jarurat nahi tha uska binna bhi ho jata becouse loop me bhi same conddition hai or isliya if condition likhna ki koi jarurat nahi thi
@abhirajchatterjee6965
@abhirajchatterjee6965 18 күн бұрын
This partitioning algorithm seems to be incorrect, say we take the same example provided in the video wherein the last element is always selected as the pivot - 10, 80, 30, 90, 40, 50, 70 | p = 70, s is at 10, e is at 70 now, s moves till 80 where it finds a violation (element at s should be less than 70, here it is > 70), e stays at 70 since it is also a violation(element at e should be greater than 70, here it is = 70 ), swapping happens -> array after first swap -> 10, 70, 30, 90, 40, 50, 80 -> s moves to 30, e moves to 50 notice that the new part of the array which is going to be partitioned now is 30,90,40,50 and the rest is assumed to have been placed correctly w.r.t the pivot that is 70, which is wrong as 70 is clearly not at the position where it is supposed to be.
@gauravgautam5963
@gauravgautam5963 2 жыл бұрын
Hey , pls tell me one thing that how the primary condition is working , that break condition , that if(low>=hi) return ;
@vinathasree168
@vinathasree168 2 жыл бұрын
Would u pls make a tutorial on regex
@RaviPatel-rj1re
@RaviPatel-rj1re 2 жыл бұрын
this quick sort algorithm not given correct ans for (5,4,1,2,8,7,6,3) can you explain it why?
@amine_fadssi
@amine_fadssi Жыл бұрын
use variable int pivot = arr[mid] not just compare with arr[mid] directly
@gourabmukherjee4848
@gourabmukherjee4848 Жыл бұрын
How to find the index of the pivot element by using this algorithm ?
@suryasrinivasb1629
@suryasrinivasb1629 2 жыл бұрын
If I use any other element as pivot instead of middle element, it gives stack overflow error. Can someone help me through this issue?(same algorithm)
@programmer6953
@programmer6953 Жыл бұрын
At 23:07 i dont get why we put pivot at middle while showing t(n) calculation ,at t=0 the left will be empty but right will be n-1 element does it mean we picked first element as pivot ?if not why we showing pivot in t(n) calculation why not just t k +t n-k + alfa n supposedly pivot is at the end of the array for better presentation pls enlighten me on this..
@swathimarkandan375
@swathimarkandan375 2 жыл бұрын
Just wow
@dinbandhusharma4568
@dinbandhusharma4568 9 ай бұрын
thanks for this amazing knowledge I really like your video 🤗🙏🏻
@abhishekh_p2721
@abhishekh_p2721 Жыл бұрын
Can someone suggest can we take mid ele as pivot
Merge Sort Using Recursion (Theory + Complexity + Code)
49:47
Kunal Kushwaha
Рет қаралды 218 М.
Recursion Subset, Subsequence, String Questions
1:23:56
Kunal Kushwaha
Рет қаралды 291 М.
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 116 МЛН
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 29 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 13 МЛН
Llegó al techo 😱
00:37
Juan De Dios Pantoja
Рет қаралды 62 МЛН
Solve Any Pattern Question With This Trick!
57:20
Kunal Kushwaha
Рет қаралды 2,3 МЛН
Quicksort Sort Algorithm in Java - Full Tutorial With Source
24:58
Coding with John
Рет қаралды 236 М.
10 Sorting Algorithms Easily Explained
10:48
Coding with Lewis
Рет қаралды 44 М.
Quick Sort For Beginners | Strivers A2Z DSA Course
35:17
take U forward
Рет қаралды 339 М.
Binary Search Interview Questions - Google, Facebook, Amazon
4:01:46
Kunal Kushwaha
Рет қаралды 1,2 МЛН
2.6.3 Heap - Heap Sort - Heapify - Priority Queues
51:08
Abdul Bari
Рет қаралды 2,1 МЛН
6.13 Dijkstra Algorithm | Single Source Shortest Path| Greedy Method
34:36
Jenny's Lectures CS IT
Рет қаралды 1,4 МЛН
World’s Largest Jello Pool
01:00
Mark Rober
Рет қаралды 116 МЛН