Quicksort In Python Explained (With Example And Code)

  Рет қаралды 130,002

FelixTechTips

FelixTechTips

Күн бұрын

Quicksort is an efficient sorting algorithm with O(n*logn) average running time. In this video I show you a quick example and how to implement this algorithm in Python step by step.
This video is part of the basic algorithms in Python playlist. The goal is to get an understanding of basic computer science algorithms and their implementation in Python.
For more coding videos subscribe to my youtube channel.

Пікірлер: 77
@1Lia11
@1Lia11 5 күн бұрын
i've been searching for hours for a video that could explain exactly what happens when there is only 2 elements, and i finally got it! thanks a lot
@zaeemjaved6850
@zaeemjaved6850 Жыл бұрын
Of all the videos I watched on youtube for quick sort, I understood this one a lot better in a much more intuitive way.
@DanielSmith-uj7rr
@DanielSmith-uj7rr 2 жыл бұрын
Very best explanation of the quick sort. Thank you. Please ignore the Dislikes and Keep it up bro. You did an amazing job to clear the understanding of this algorithm. Thank you again!
@stumbleio
@stumbleio 4 ай бұрын
Best explanation on KZbin. You should continue making these types of videos. You do such a great job at it!
@LanaDelReyFan1998
@LanaDelReyFan1998 2 жыл бұрын
this entire playlist saved me great deal of time. thank you very much!
@binkarimov4218
@binkarimov4218 3 ай бұрын
Bro you gave me full understanding of quick sort! Thank you! Please continue to make such videos with other sortings in Python!
@ganeshreddykomitireddy5128
@ganeshreddykomitireddy5128 Жыл бұрын
one of the finest explanations found on youtube.
@syydsalman
@syydsalman 2 жыл бұрын
Masha"Allah Felix, This took me a day to understand, you video helped me alot
@TheKorkahn
@TheKorkahn 3 жыл бұрын
nice smoothing voice, thanks for the explanation FelixTechTips!!
@henryconner780
@henryconner780 12 күн бұрын
Thanks bro, really great visual. You saved me a lot of time
@polimorphic13
@polimorphic13 Жыл бұрын
The graphic explanation is perfect.
@LukeAvedon
@LukeAvedon Жыл бұрын
LOVE THE SWAP ANIMATION!
@liabasqulizad7962
@liabasqulizad7962 2 ай бұрын
Thank you sir. That was very good explanation.
@ibrohimahmadjonov6859
@ibrohimahmadjonov6859 Жыл бұрын
Thank you for so comprehensive explanation
@creative_py9169
@creative_py9169 2 жыл бұрын
Very good explanation
@potlurisairaj6669
@potlurisairaj6669 Жыл бұрын
you saved my time thank you
@olafschlammbeutel
@olafschlammbeutel 3 жыл бұрын
Nice animations!
@AM-nl5yo
@AM-nl5yo 2 жыл бұрын
Thank you, this helps me ✨
@maxfrischdev
@maxfrischdev 6 ай бұрын
what ever your reason was, that made you stop making videos. 1. Thanks for making them, really well explained! Hut ab! 2. The market for tutorial creators might seem "overcrowded" but really sad that you stopped, I think you have great potential! Have a good life, mate! 🤘
@FelixTechTips
@FelixTechTips 5 ай бұрын
Vielen vielen Dank für die nette Worte. Ich hatte wenig Zeit in den letzten Jahren, aber ich plane ab 2024 weiterzumachen :) Grüße nach Indonesien :)
@maxfrischdev
@maxfrischdev 5 ай бұрын
@@FelixTechTips dann wünsche ich dir einen starken Start im neuen Jahr! 💪🏻🤘🏻🤘🏻🤘🏻🤘🏻😁
@Mc-os1yc
@Mc-os1yc 2 жыл бұрын
In your visual explanation, you mentioned a scenario where j < i. That will never happen in your code because both while loops break when i=j. Anyway, it's a good vid nonetheless. thanks
@jeanluc9129
@jeanluc9129 Жыл бұрын
I don't think that's right. Only the outer loop will break when i=j
@allandogreat
@allandogreat 2 жыл бұрын
good works
@ahmet8266
@ahmet8266 2 жыл бұрын
I got it thank you
@aungkyawkhant321
@aungkyawkhant321 6 ай бұрын
Incredible explanation!
@FelixTechTips
@FelixTechTips 5 ай бұрын
Thank you :)
@darcash1738
@darcash1738 2 ай бұрын
Could we also make the right endpoint just the very end of the array and the left at index 0? Or how would we do it if a different pivot was used?
@logofios
@logofios Жыл бұрын
Cool!
@user-hn5vk7ny6i
@user-hn5vk7ny6i 4 ай бұрын
Hey !your explanation is great.one quick question Python has line by line interpreter So,when we call partition function won't that cause declaration error because it should be declared first before calling...
@jasmineshaik7025
@jasmineshaik7025 10 ай бұрын
Super.....but ur code should be zoomed it will look better anyway nice ❤️
@SpencerBoucher
@SpencerBoucher 2 жыл бұрын
I'm having trouble understanding one thing. After i and j meet, shouldn't we just unconditionally exchange arr[i] and the pivot? Why do we check that arr[i] > arr[right] at that point? Shouldn't the pivot be placed at that point that i and j meet in order to be properly sorted?
@manojkarthik6158
@manojkarthik6158 2 жыл бұрын
Same doubt bro
@jacqueline2330
@jacqueline2330 2 жыл бұрын
i think the check is considering the case that only TWO elements were left. we always pick up the last element as our pivot, and the remaining one will become the one=i=j automatically. so we should check it first before we swap them. Like in the video 8:27, what if we have 77 88 left already inplace instead of 88 77?
@andresc.56
@andresc.56 7 ай бұрын
ur awesome
@jenniferr3204
@jenniferr3204 5 ай бұрын
Thank you! Your explanations were very good. How would I change the code for parallel quickSort? Is dynamic parallel quickSort the same as just parallel quickSort?
@darcash1738
@darcash1738 2 ай бұрын
I’m not sure what this means but this sounds like you could know the answer to this. Could we also make the right endpoint just the very end of the array and the left at index 0? Or how would we do it if a different pivot was used?
@DJsteuph
@DJsteuph 4 ай бұрын
when i does not get incremented if the while loop does not run at all initially, that then creates a negative -1 right parameter in the quicksort(arr, left, partition_pos-1) ,where partition_pos=0, function later on. Why isn't this a problem?
@samuelvalentine7846
@samuelvalentine7846 Жыл бұрын
For me, i found another way to do the partitioning using a for loop in python def partition(arr, high, low=0): if not arr and not high: return 'No array and no upperbound' pivot = arr[high] partion_at = low for j in range(low, high): if arr[j]
@atharvachouhan474
@atharvachouhan474 11 ай бұрын
No
@samuelvalentine7846
@samuelvalentine7846 11 ай бұрын
@@atharvachouhan474 no what?
@atharvachouhan474
@atharvachouhan474 11 ай бұрын
@@samuelvalentine7846 yes
@abhiprit20
@abhiprit20 2 жыл бұрын
Instead of passing list as input, if tuple, dictionary, set is passed. So will the time complexity of the algorithm remains same or will differ? Thanks,
@jesmigeorge4936
@jesmigeorge4936 2 жыл бұрын
Now this won't work on tuple i suppose because tuples are immutable and in this algorithm we swap and make changes in place itself. So.. Nope it won't work for tuples.it would give us error... So it's time complexity might not be there with tuples.
@Hex-Scholar
@Hex-Scholar Жыл бұрын
Could you please explain me what will the space complexity be ?
@PureCrimsonLotus
@PureCrimsonLotus Жыл бұрын
At 11:08, you say "the j that defines the point right of the pivot," but if the pivot is always the last element in the array, nothing can be to its right. Did you mean "the point left of the pivot"?
@zakthesquirrel7621
@zakthesquirrel7621 7 ай бұрын
i think he wanted to say "right next to the pivot"
@alex_reye
@alex_reye 2 жыл бұрын
Thank you so much for the explanation! By the way, how should this algorithm be implemented with random element chosen as the pivot?
@anirudhsoni6529
@anirudhsoni6529 2 жыл бұрын
we could use any element as pivot in this case he has taken the last one
@darcash1738
@darcash1738 2 ай бұрын
@@anirudhsoni6529would we put the right pointer at the very end of the array instead of how he has it here, where it is just left of the last element?
@ejazxdd
@ejazxdd Жыл бұрын
Kalander op
@ermiasendale
@ermiasendale 6 ай бұрын
Why don't we use "if array[i] > pivot and i > j "# indicating that they have crossed each other instead of "if array[i] > pivot:" since our objective is to handle the scenario where they cross each other?
@liiiaaaaammm
@liiiaaaaammm Ай бұрын
cual es la condicion de parada para la recursion?
@shootanees150
@shootanees150 Жыл бұрын
Dadu pashu 😍😍😍😍
@user-qb5tg6eg2e
@user-qb5tg6eg2e 2 жыл бұрын
13:36 my own video mark
@mohamed5986
@mohamed5986 Жыл бұрын
i faced a problem in it that quick sort isn't quick with me at all idk why it doesn't work i guess my laptop is kinda weak
@mbappekawani9716
@mbappekawani9716 3 жыл бұрын
j can be less than i???
@theInspireVista
@theInspireVista 10 ай бұрын
Can you please upload the quick sort for worst case scenario with O(n^2) ?
@davidr.603
@davidr.603 9 ай бұрын
That happena when the pivot element is either really big or really small not sure which it was, maybe both
@the_gadfly5717
@the_gadfly5717 Жыл бұрын
What if there are duplicates?
@rdanimetalks7964
@rdanimetalks7964 Жыл бұрын
Sirr where r urrr Videos... it's been a yearr...Did u quit???? 😭😭😭
@rpalanivel83
@rpalanivel83 Жыл бұрын
Thanks for the wonderful video. The code was not worked for me. It went infinite loop. I fixed the issue by adding i += 1 and j -= 1 after arr[i], arr[j] = arr[j], arr[i]
@shootanees150
@shootanees150 Жыл бұрын
Qulandar
@nkwellekwo8051
@nkwellekwo8051 Жыл бұрын
Please why is J right -1?
@kummaridharmateja9593
@kummaridharmateja9593 Жыл бұрын
right means size of array i.e, 8 here index will start from 0th so array[last_ment] =>array[8] it means we dont have 8th element in array thats why right -1
@trl2006
@trl2006 Жыл бұрын
Why return i?
@shashankdevadiga2646
@shashankdevadiga2646 8 ай бұрын
I think it's ' j= right '
@matissjansons8789
@matissjansons8789 4 ай бұрын
how about this: def quicksort(arr): if len(arr) pivot] return quicksort(left) + middle + quicksort(right)
@walternathaniel7365
@walternathaniel7365 2 жыл бұрын
If you run the sorting function, the result is None. This is because the sorting function does not Return anything. You may want to edit your code.
@kimstuart7989
@kimstuart7989 2 жыл бұрын
works fine for me. It isn't returning anything because this algorithm is designed to be in-place and it is just modifying the original array itself. print your array, call quick_sort(), then print your same array again and you will see it sorted. If you want, you can add return arr at the end of your quick_sort function, then in your main call it using sorted_array = quick_sort(), but it's not necessary.
@reo4465
@reo4465 Жыл бұрын
it will work just fine cause he is sending list as a parameter and that is passed by reference
@aids_47_aditishetty84
@aids_47_aditishetty84 2 жыл бұрын
unbound local partition error
@shootanees150
@shootanees150 Жыл бұрын
Dadu
@THE-MNG
@THE-MNG 2 жыл бұрын
I got an error when I use 100 elements of array
@Mc-os1yc
@Mc-os1yc 2 жыл бұрын
I suppose it's stack overflow cuz it hits stack size of your system
@lucianaferrand64
@lucianaferrand64 Жыл бұрын
ok, I posted a comment before that was incorrect. I was missing a piece of code - Works wonderfully - Here is another example in python of quickSort application Check my function below and works for any example of an array. def quickSort(dataset, first, last): if first < last: pivotIdx = partition(dataset, first, last) # now sort the two partitions quickSort(dataset, first, pivotIdx-1) quickSort(dataset, pivotIdx+1, last) def partition(datavalues, first, last): # choose the first item as the pivot value pivotvalue = datavalues[first] # establish the upper and lower indexes lower = first + 1 upper = last # start searching for the crossing point done = False while not done: # TODO: advance the lower index while lower = lower: upper -= 1 # TODO: if the two indexes cross, we have found the split point if upper < lower: done = True else: # if they haven't cross each other temp = datavalues[lower] datavalues[lower] = datavalues[upper] datavalues[upper] = temp temp = datavalues[first] datavalues[first] = datavalues[upper] datavalues[upper] = temp # return the split point index return upper # test the merge sort with data print(items) quickSort(items, 0, len(items)-1) print(items)
Breadth First Search Algorithm Explained (With Example and Code)
8:08
Selection Sort In Python Explained (With Example And Code)
8:27
FelixTechTips
Рет қаралды 53 М.
NO NO NO YES! (50 MLN SUBSCRIBERS CHALLENGE!) #shorts
00:26
PANDA BOI
Рет қаралды 95 МЛН
How many pencils can hold me up?
00:40
A4
Рет қаралды 11 МЛН
О, сосисочки! (Или корейская уличная еда?)
00:32
Кушать Хочу
Рет қаралды 6 МЛН
ШЕЛБИЛАР | bayGUYS
24:45
bayGUYS
Рет қаралды 688 М.
Lec-44: INSERTION SORT in PYTHON 🐍 | DSA Concepts in Python 🐍
8:58
Merge Sort In Python Explained (With Example And Code)
13:35
FelixTechTips
Рет қаралды 189 М.
Learn Quick Sort in 13 minutes ⚡
13:49
Bro Code
Рет қаралды 273 М.
Quick Sort - Data Structures & Algorithms Tutorial Python #15
31:17
Quicksort Algorithm: A Step-by-Step Visualization
9:32
Quoc Dat Phung
Рет қаралды 33 М.
Merge Sort in Python Programming | Program | Detailed Explanation
32:42
Amulya's Academy
Рет қаралды 95 М.
Quick Sort Theory | DSA
21:18
Telusko
Рет қаралды 13 М.
iPhone green Line Issue #iphone #greenlineissue #greenline #trending
0:10
Rk Electronics Servicing Center
Рет қаралды 4,7 МЛН
📱 SAMSUNG, ЧТО С ЛИЦОМ? 🤡
0:46
Яблочный Маньяк
Рет қаралды 1,4 МЛН
iPhone 15 Pro vs Samsung s24🤣 #shorts
0:10
Tech Tonics
Рет қаралды 4,1 МЛН
Vortex Cannon vs Drone
20:44
Mark Rober
Рет қаралды 14 МЛН
Very Best And Good Price Smart Phone
0:42
SDC Editing Zone 9K
Рет қаралды 216 М.