walkccc / CLRS

📚 Solutions to Introduction to Algorithms Third Edition
https://walkccc.me/CLRS
MIT License
4.65k stars 1.26k forks source link

Exercise 7.1: Solution doesn't guarantee correctness of partition in general case. #394

Open AjGodara opened 3 years ago

AjGodara commented 3 years ago

AS LONG AS ALL THE ELEMENTS ARE EQUAL, we will return the mid-point, which is not a problem . But consider that 40% of the elements are equal to the pivot. Now, by the time we reach the end of partition loop, all 40% will be in the first partition(before i). And also assume that 50% more are less than pivot. So the returned index would have been at 90% of the distance between start and end. Now we decrease the returning index by 40/2 = 20%. Instead of @90%, we'll now return the index at 70% of the length. But this is problematic. Because