I have an array of N numbers which are same.I am applying Quick sort on it.
What should be the time complexity of the sorting in this case.
I goggled around this question but did not get the exact explanation.
Any help would be appreciated.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This depends on the implementation of Quicksort. The traditional implementation which partitions into 2 (
<and>=) sections will haveO(n*n)on identical input. While no swaps will necessarily occur, it will causenrecursive calls to be made – each of which need to make a comparison with the pivot andn-recursionDepthelements. i.e.O(n*n)comparisons need to be madeHowever there is a simple variant which partitions into 3 sets (
<,=and>). This variant hasO(n)performance in this case – instead of choosing the pivot, swapping and then recursing on0topivotIndex-1andpivotIndex+1ton, it will put swap all things equal to the pivot to the ‘middle’ partition (which in the case of all identical inputs always means swapping with itself i.e. a no-op) meaning the call stack will only be 1 deep in this particular case n comparisons and no swaps occur. I believe this variant has made its way into the standard library on linux at least.