Top Comments
All Comments (82)
-
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...
-
Umm no. Has nothing to do with "worst case scenario".
-
@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.
-
Yeah man. You're right! Good catch.
-
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.
-
@unrealhacker12 yea its called the worst case scenario
-
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.
-
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.
-
you are the man! thank you dude!
could you please let me know how did you make this video? what program did you used?



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 72
u save me dude!!!!
shion16 2 years ago 53