# Time complexity of quicksort for arrays in increasing or descreasing order

Two $$n$$-size arays are given: $$n_1$$ is in decreasing order and $$n_2$$ is in increasing order.

Let $$c_1$$ be the time complexity for $$n_1$$ using quicksort, and $$c_2$$ the time complexity for $$n_2$$ using quicksort.

I think $$c_1 = c_2$$ and $$c_1=O(n^2)$$? Is this correct ?

I am using the last element as a pivot for each partition.

• The complexity of Quicksort depends on the pivot element that you've chosen. – Gokul Feb 17 at 10:32
• lets say I am choosing last element as a pivot – Manoharsinh Rana Feb 17 at 10:34
• That's a known bad strategy. Don't do that. Now think about how you would sort faster if a reasonable percentage of arrays that you sort are already sorted. – gnasher729 Feb 17 at 11:10
• The question isn't answerable from the information given in your post. The answer depends on how you choose the pivot element. Just saying "quicksort" doesn't fully specify the algorithm you're using. Please edit the question to specify exactly what algorithm you're using. Don't just leave information in the comments; edit the question to make sure that everything that people need to answer is found in the question. – D.W. Mar 7 at 19:44

## 2 Answers

Quicksort is just an idea. There are algorithms and implementations.

Only stupid implementations will run in O(n^2) for sorted or reversed sorted arrays. Good sorting implementations actually check for elements in ascending or descending order at the beginning and end of the array and will handle either case in O(n), because wanting an array sorted without checking that it is sorted before is such a common case. Such implementations will fall back on Quicksort for handling the more difficult parts.

(It’s not just sorted arrays. You can have a general algorithm that sorts the concatenation of two sorted arrays in O(n), or a sorted array with fewer than O(n / log n) random elements added, or a sorted element with one random element changed).

No, C1 =/= C2. Time complexity of quick sort depends on selection(position) of pivot element.Best case,Avg. case and worst case of various sorting technique are as: 