QuickSort - Python

One test case is giving tle. please can you tell why?

Hey @Anshit, there is no need to use the partition function separately. You can simply use quickSort() method to call the partition_r() method. Try not to use the partition function separately and then I guess your code should be good to go !

Hope this helps.
Happy Coding :slight_smile:

I tried it. It still isnt working…

Hey @Anshit, I don’t get it why have you used the random.randint() function ? Can you explain me once ? Because according to me you can simple skip those lines of code.

Because if the array is sorted then it is the worst case for quicksort
so it is ensuring that it is not sorted
Also the question says we have to use randomized quicksort

I have tried removing this but the second case still gives tle

Okay now listen to me :

  1. random.randint( ) would return a random number between start and end. Then in the next line you are replacing that random element between s and e, with the last element of the list…which again I think is not right for normal quicksort. (I guess you are confusing the concept with randomized pivoting. In that case your logic of code also has to change)
  2. Value of i cannot always be -1. It would be -1 only in the case when you pass the complete list to this partition_r() .
  3. Similarily, value of j also cannot be always 0. Please do not hard code. Try to generalize your code.
  4. Also the name of your list is l or list ?? Check the parameters once.

Try resolving these issues. You would be able to get the correct answer. If you are still stuck at any step, you can revert back anytime. I would provide you with the correct code.

Happy coding :slight_smile:

The 2nd point was the reason…its running now…thank you