Quicksort can be modified to have a worst-case runtime of O(n log n) by choosing the pivot in a way that selects the median of a sample of elements, taking linear time. This guarantees the two partitions will be of size n/4 and 3n/4 in the worst case, resulting in the recurrence T(n)=T(n/4)+T(3n/4)+O(n) which solves to O(n log n). However, this comes at the cost of average-case performance, as the constant factor is significantly higher than standard Quicksort.
Quicksort can be modified to have a worst-case runtime of O(n log n) by choosing the pivot in a way that selects the median of a sample of elements, taking linear time. This guarantees the two partitions will be of size n/4 and 3n/4 in the worst case, resulting in the recurrence T(n)=T(n/4)+T(3n/4)+O(n) which solves to O(n log n). However, this comes at the cost of average-case performance, as the constant factor is significantly higher than standard Quicksort.
Quicksort can be modified to have a worst-case runtime of O(n log n) by choosing the pivot in a way that selects the median of a sample of elements, taking linear time. This guarantees the two partitions will be of size n/4 and 3n/4 in the worst case, resulting in the recurrence T(n)=T(n/4)+T(3n/4)+O(n) which solves to O(n log n). However, this comes at the cost of average-case performance, as the constant factor is significantly higher than standard Quicksort.
Quicksort can be made in the worst case (nlgn) by choosing
the pivot in the same way as in the worst case linear selection for the i-th smallest key. Recall that the pivot there was defined equal to the median of the sequence with n/5 elements constructed from medians of 5-element groups. Using the worst-case linear algorithm to select the median of the sequence of medians, the work needed to choose the pivot will take linear time. The worst case for Quicksort at a pivot chosen in this a way will occur when the partition produces the most unbalanced subranges of sizes n/4 and 3n/4. So, the recurrence for the worst case is: W(n)=W(n/4)+W(3n/4)+ (n), which yields W(n)=(n lgn) (Why?). Note: In this way we improve the worst case for Quicksort, but lose in the average behavior since it becomes cnlgn with c significantly exceeding 1.386.