Worst case maybe N^2, but don't forget that if you shuffle the array(before any sorting) and pick the median as the pivot, then use insertion sort for small enough sections, you can avoid N^2 with a very high probability, getting closer to the best case, Nlg(N). This is why this algorithm is widely used in the industry...
I don't see how this algorithm would work, if 17, 32, 29, 17, 37, 15, 10, 42, 15,17 if the pivot was the last element 17 then it would get caught in a loop indefinitely swapping 17 with 17 as both are equal to the pivot.
not really, what you are talking about is the worst case scenario. However, it will not be infinite loop. But it will sort the items one at a time costing O(n^2) "quadratic complexity"
swapping 17 with another 17 can happen only once because after that, the two pointers which are doing the swaps will intersect and that is a stop condition for the q sort algorithm.
By the way this is the third method to do a quick sort.
In this case, there will be an infinite loop, because the 17's will swap back and forth indefinitely WITHOUT ADVANCING THE CURSORS. See the line of code that advances the L cursor "while (A[L] < pivot) L = L + 1". Now substitute the value 17 for A[L] and pivot. Notice condition will always be false, therefore cursor will not advance. Same story with the R cursor.
I may be wrong but to me this title is misleading. This is the in-place Quick sort. There is another form of quick sort that I am studying that uses a tree. If I am correct on this then the title should probably be changed. I almost second guessed myself until I consulted my book.
@KHCshadow1 this can only happen when L and U are both pointing to the pivot. at this point, it would swap the element with itself and then exit the while loop.
@vileguy If you have a value in your array that's equal to the pivot it will bring this into an infinite loop because it just constantly swaps it with either the pivot or yet another value equal to the pivot. A. For example, in the original example of this video replace both the 86 and the 3 with 34 and think through the algorithm (the same thing will happen if you replace only the 3 or only the 86 with 34, it will just take longer to get to the infinite loop conditions).
@KHCshadow1 The algorithm swaps an element that is less than or equal to the pivot on one side or greater than or equal to the pivot on the other side. If the there are 3 elements with the same value, 1 being the pivot, it will just swap them. After it swaps, the cursors move forward. You could have redundant switching with duplicate elements, but it wouldn't get stuck in an infinite loop if you write it properly. Additionally, the data being sorted often has distinct values which prevents dupes
@nolliepoper if you're getting divide by zero then make sure size isn't zero. taking the middle element and a random element are equivalent with a random list, but some lists you want to sort may have a start state that could make taking the middle element close to the worst-case. you don't have to pick your pivot at random, it may be more effective to pick it systematically, but you have to consider what type of list you're sorting.
@filkarada Because 5 was selected as the pivot of the left side of the array. So, after partitioning that side, all the values lower than 5 go to its left.
@prettatva Nothing. From Wikipedeia(step 2): "2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way)."
The algorithm presented at the end of the video has a small bug. This alg works fine when all array elements are unique. In the case that the array has a duplicate element, and the pivot happend to be set to one the duplicate elements, then the bigger while loop hangs indefinately. This statement in the bigger while is needed to fix this bug.
if (pivot == Array[lower] && pivot == Array[upper])
Quicksort is considered the fastest sorting algorithm on current computers (it hits caches better than merge sort). On the other hand bubble sort is veery slow and requires a lot of comparisons.
You get less average and worst-case-scenario steps.. In algorithms its all about the number of steps... If you have n elements to sort, bubblesort sorts them in n^2 steps, while quicksort sorts them in less than nlogn most of the time
Comment removed
mnthol 1 month ago
Worst case maybe N^2, but don't forget that if you shuffle the array(before any sorting) and pick the median as the pivot, then use insertion sort for small enough sections, you can avoid N^2 with a very high probability, getting closer to the best case, Nlg(N). This is why this algorithm is widely used in the industry...
tristantao 1 month ago
I don't see how this algorithm would work, if 17, 32, 29, 17, 37, 15, 10, 42, 15,17 if the pivot was the last element 17 then it would get caught in a loop indefinitely swapping 17 with 17 as both are equal to the pivot.
unrealhacker12 1 month ago
@unrealhacker12
not really, what you are talking about is the worst case scenario. However, it will not be infinite loop. But it will sort the items one at a time costing O(n^2) "quadratic complexity"
swapping 17 with another 17 can happen only once because after that, the two pointers which are doing the swaps will intersect and that is a stop condition for the q sort algorithm.
By the way this is the third method to do a quick sort.
illusionist50 1 month ago
@illusionist50
In this case, there will be an infinite loop, because the 17's will swap back and forth indefinitely WITHOUT ADVANCING THE CURSORS. See the line of code that advances the L cursor "while (A[L] < pivot) L = L + 1". Now substitute the value 17 for A[L] and pivot. Notice condition will always be false, therefore cursor will not advance. Same story with the R cursor.
uyensa 1 month ago
@unrealhacker12 yea its called the worst case scenario
VinUnleaded182 1 month ago
@VinUnleaded182
Umm no. Has nothing to do with "worst case scenario".
uyensa 1 month ago
@unrealhacker12
Yeah man. You're right! Good catch.
uyensa 1 month ago
you are the man! thank you dude!
could you please let me know how did you make this video? what program did you used?
kaigamwasdf 1 month ago
Thank you very much for your videos
Very helpfull!
kefkeuh 2 months ago
very nice! thx
seyas85 2 months ago in playlist Weitere Videos von xoaxdotnet
superb video....thnx...
coolnics11 2 months ago
I may be wrong but to me this title is misleading. This is the in-place Quick sort. There is another form of quick sort that I am studying that uses a tree. If I am correct on this then the title should probably be changed. I almost second guessed myself until I consulted my book.
Metalgod89 2 months ago
Thanks. This really helped to clear my confusions.
zskhan47 3 months ago
Great explanation. Very Clear and precise. Thanks.
patchtok 3 months ago
thank you! very useful and informative vid!
pooyasami 3 months ago
What happens if A[L] and A[U] are both equal to the pivot...
KHCshadow1 3 months ago
@KHCshadow1 this can only happen when L and U are both pointing to the pivot. at this point, it would swap the element with itself and then exit the while loop.
vileguy 3 months ago
@vileguy If you have a value in your array that's equal to the pivot it will bring this into an infinite loop because it just constantly swaps it with either the pivot or yet another value equal to the pivot. A. For example, in the original example of this video replace both the 86 and the 3 with 34 and think through the algorithm (the same thing will happen if you replace only the 3 or only the 86 with 34, it will just take longer to get to the infinite loop conditions).
KHCshadow1 3 months ago
Its a bug in the code
KHCshadow1 3 months ago
@KHCshadow1 The algorithm swaps an element that is less than or equal to the pivot on one side or greater than or equal to the pivot on the other side. If the there are 3 elements with the same value, 1 being the pivot, it will just swap them. After it swaps, the cursors move forward. You could have redundant switching with duplicate elements, but it wouldn't get stuck in an infinite loop if you write it properly. Additionally, the data being sorted often has distinct values which prevents dupes
vileguy 3 months ago
@vileguy "After it swaps, the cursors move forward."
You are assuming unique values. KHCshadow1 was talking about duplicate values, which will produce an infinite loop.
uyensa 1 month ago
Comment removed
9468859 3 months ago
love the background music
cjfong425 4 months ago
Napoleon Dynamite ?
himiker 4 months ago in playlist Java Algorithm Tutorial
It is very useful and informative .. it really helps me alot .. Thanks
zigzagnet1 5 months ago
thx great video - but how do i analyze the runtime of the algorithm - i know that the average case is O(n*log(n)) but how do i get there?!
konsu89 5 months ago
I like how you say pipet
deadlybug 6 months ago
Comment removed
apoorvaprabhusg 6 months ago
pivot = nums[rand() % size] was giving me a dividing by zero error. So I use "pivot = nums[size / 2]"
nolliepoper 6 months ago
@nolliepoper if you're getting divide by zero then make sure size isn't zero. taking the middle element and a random element are equivalent with a random list, but some lists you want to sort may have a start state that could make taking the middle element close to the worst-case. you don't have to pick your pivot at random, it may be more effective to pick it systematically, but you have to consider what type of list you're sorting.
vileguy 3 months ago
finally i understood how Quicksort works, thanks for lesson
spdzodzo 7 months ago
Refreshing my mind for my texes exam and this definitely helps thank you
TheonEvilbuho 7 months ago
8 losers live in a cave and have no knowledge abt computers
amoghavarsha1990 7 months ago 2
thanks dude!
phonebooth19 7 months ago
Nice video, but i'm going to sleep now
iRouRoui 7 months ago
this fucking song is annoying, but good vid.
0dark 8 months ago
At 2:40 why did the 3 swap with the 5?
filkarada 8 months ago
@filkarada Because 5 was selected as the pivot of the left side of the array. So, after partitioning that side, all the values lower than 5 go to its left.
Atrofiado91 7 months ago
Super helpful .. awesome teaching.
god bless u.
nik1rookie 8 months ago
ZaaoooZaaa
crazyidiot101 8 months ago
1:49 According to your explanation, those two elements must've been switched as well, but you aren't doing so. Why?
Herrgolani 8 months ago
Very helpful, thank you!
Rebbirth 8 months ago
thank you !!
Farmerbr0s 9 months ago
Algorithm doesn't work for duplicate values.
rival843 9 months ago
Thank you! Was struggling to understand how this sorting works.
johnxxx9 9 months ago
Nice. Love the music, too ;)
Krimson444 10 months ago
thanks a lot! I'm one of those who would dig into the books and understand absolutely nothing until I see how it works with my own eyes :)
AntonTheOmniscient 1 year ago
Its amazing how much a simple video can explain something so difficult.
LazyHermit 1 year ago 2
Pivot can also the length of A // 2. //= will give you an int.
jayvanjj 1 year ago
i didnt find a better explanation thanxxx!!!!!!
felfaj 1 year ago
Very good tutorial for quicksort. This video explains a lot.
kamillys 1 year ago
awesome !!
praslisa 1 year ago
THUMBS-UP! you're the best man! ;)
vuraboz 1 year ago 3
what happens if the arry doesnt have unique elements?
prettatva 1 year ago
@prettatva write this:
while(A[L]<= pivot)
bunower 1 year ago
@prettatva Nothing. From Wikipedeia(step 2): "2. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way)."
pithikoulis 11 months ago
Best explanation so far!
ivanatomic 1 year ago
The algorithm presented at the end of the video has a small bug. This alg works fine when all array elements are unique. In the case that the array has a duplicate element, and the pivot happend to be set to one the duplicate elements, then the bigger while loop hangs indefinately. This statement in the bigger while is needed to fix this bug.
if (pivot == Array[lower] && pivot == Array[upper])
{ lower++; // move either lower up, or upper low
}
afarhangi 1 year ago 10
very clear explanation, as always.
tivrfoa 1 year ago 4
Thank you!
AleAleDane 1 year ago
wonderful video 5*
Untitledwiz 1 year ago 2
thanks - best explanation I've seen - quick, to the point and short and clear pseudocode.
Suyamu 2 years ago 5
thanxxxxxxxx by watching this video all algo becomes easy to understand
praveenmandloi51 2 years ago 3
Hey guys, thanks for this set of videos on sorting algorithms. Great jog!!!
lelinhosgp 2 years ago 4
great video! Keep working!
Hoogoo123 2 years ago 4
The pseudocode was particularly helpful. I am writing an algorithms exam tomorrow, and this video's helped me make sure I know what's going on.
0xMal 2 years ago 69
u save me dude!!!!
shion16 2 years ago 53
i think this last 2 videos i have seen (merge and quick) should show the O(nlnn) stuff
MarcosMora 2 years ago 2
great video, make it very simple to understand and implement ! :)
heezhot 2 years ago 3
What is the purpose of the quick sort? Why not use a bubble sort?
FromHeavenBaby 2 years ago
Quicksort is considered the fastest sorting algorithm on current computers (it hits caches better than merge sort). On the other hand bubble sort is veery slow and requires a lot of comparisons.
Kulik0 2 years ago
that's because quick sort is better then bubble sort..
onegor 2 years ago
bubble sort is O(n^2) on avarage, quick sort is O(n*lg(n)) on avarage.
hgeithus 2 years ago
You get less average and worst-case-scenario steps.. In algorithms its all about the number of steps... If you have n elements to sort, bubblesort sorts them in n^2 steps, while quicksort sorts them in less than nlogn most of the time
Givicencio 2 years ago
Because quick sort is much faster than bubble sort when the number of elements to be sorted increases.
hyperstation123321 2 years ago
thank you for the great vids
taitaisparks 2 years ago
awesome keep 'em coming!! !!!!!! ... . . .. !
TheBatchGuy 2 years ago
thx for this
Globox822 2 years ago