GeeksforGeeks » Interview Questions
datastructure
(4 posts)-
what is the complexity of quick sort when all elements are equal
-
it's a tricky condition. if all the elements are equal, then all elements will be equal to the pivot. Then we have to decide whether we will swap the elements or we will not. If we decide not to swap the elements in case of equal, then we end up with almost all the elements either in subset one or subset two depending on the implementation. Then again on the subset one or subset two we will start the same process, resulting process will become n^2 complexity. but if we decide to swap the elements, then we will end up with two equal size subsets. So basically it will divide the sets again and again in half and proceed. Resulting process has a complexity of n(logn).
-
thans
-
It all depends on how we select the pivot and what we compare when we swap the elements
Reply
You must log in to post.