walkccc / CLRS

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

Exercise 9.2.1 #303

Open rualark opened 4 years ago

rualark commented 4 years ago

This solution is wrong:

Calling a 0-length array would mean that the second and third arguments are equal Wrong, this means that third argument is less than second argument.

So, if the call is made on line 8, we would need that p = q - 1, which means that q - p + 1 = 0. Wrong, this means that q - p + 1 = 2

Alisia0182 commented 3 years ago

Yeah! I agree with you! ! Calling a 0-length array would mean that the second argument is larger than the third argument by 1, I think. So, if the call is made on line 8, we would need that p = q - 1 + 1 = q.

However, i is assumed to be a positive number, and to be executing line 8, we would need that i < k = q - p + 1 = 1,which means that i <=0, a contradiction. The other possibility is that the bad recursive call occurs on line 9. This would mean that q + 1 = r + 1. To be executing line 9, we need that i > k = q - p + 1 = r - p + 1. This would be a nonsensical original call to the array though for i should be in [1, r - p + 1].

myyoutubechannel commented 2 years ago

I also think there was a problem with the solution. However I am confused with the meaning of 0 length array. Is that means, the array is an empty array? Like there is no element in the array? If so, then second argument should be equal to the third argument right?

If I am not wrong A[q] is the pivot. So elements p to q-1 should be less than A[q], For example there are 10 elements in the array and indexing staring from 1. The q index let's say 6. So the elements on index 1 to 5 should be less than element in index 6. Now, p=1 and q-1 = 5 as per my example. Now you said for a 0 length array second argument which is p should be 1 larger than third argument which is 5. So as per my example p should be 6 and q-1=5. Then how will it work?