News for hackers. Stuff that matters.
Re:On the Worst-Case Complexity of TimSort by grillorafael on Friday August 31, 2018 @04:41AM (#17883943)
yes but they will share the same complexity
Re:On the Worst-Case Complexity of TimSort by Someone on Friday August 31, 2018 @05:04AM (#17884044)
You can make the average-case perform in O(n^2) by always pivoting on the smallest number in the array. Nobody would do _that_, but it shows that pivot choice can affect complexity.

Ergo, computer scientists researching the algorithm mathematically must consider the effect of choice of pivot.

Re:On the Worst-Case Complexity of TimSort by FreeFull on Friday August 31, 2018 @05:39AM (#17884182)
Depending on how you choose the pivot, the worst-case of complexity of Quicksort can be O(n^2) or O(n log n)
Re:On the Worst-Case Complexity of TimSort by klmr on Friday August 31, 2018 @06:44AM (#17884435)
No, worst-case complexity of real quicksort is always O(n^2), regardless of pivot choice strategy (even with stochastic pivot choice, although you’d have to get very unlucky to hit that worst case). You can make the average case better or worse though.

The only way of making quicksort’s worst-case runtime O(n log n) is by limiting recursion depth, as done e.g. in introsort. But that’s no longer quicksort.

Re:On the Worst-Case Complexity of TimSort by PL_kolek on Friday August 31, 2018 @08:09AM (#17884820)
Isn't there a linear time median selection algorithm, which allows you to always select a pivot in the middle of the sorted part and create two equal halves? This produces a worst-case O(n log n) quick sort, which is no longer quick due to the big constant hidden in O notation.
Re:On the Worst-Case Complexity of TimSort by nh2 on Friday August 31, 2018 @09:30AM (#17885402)
Correct, Quicksort with Quickselect for pivot choice.
Re:On the Worst-Case Complexity of TimSort by deathanatos on Friday August 31, 2018 @05:32PM (#17888731)
"quickselect" is a selection algorithm that uses a partial quicksort in order to do a select. You're essentially saying to write quicksort using quicksort.

Quickselect requires a pivot choosing strategy; the problem is not only the same as quicksort's, it is the problem from quicksort.

According to Wikipedia, in the worst case, it is O(n²).[1] But that's not strictly correct, IMO. Regardless, it doesn't answer the OP's question of "is there a selection algorithm that operates in worst case O(n)"

[1]: https://en.wikipedia.org/wiki/Quickselect

[2]: https://news.ycombinator.com/item?id=17888755 and the parent comment; specifically, the median-of-medians algorithm is a worst-case O(n) selection algorithm.

Re:On the Worst-Case Complexity of TimSort by nh2 on Friday August 31, 2018 @09:26AM (#17885354)
This is wrong.

See https://en.m.wikipedia.org/wiki/Quicksort, section "Selection-based pivoting".

Re:On the Worst-Case Complexity of TimSort by deathanatos on Friday August 31, 2018 @05:39PM (#17888755)
Specifically,

> A variant of quickselect, the median of medians algorithm, chooses pivots more carefully, ensuring that the pivots are near the middle of the data (between the 30th and 70th percentiles), and thus has guaranteed linear time – O(n). This same pivot strategy can be used to construct a variant of quicksort (median of medians quicksort) with O(n log n) time. However, the overhead of choosing the pivot is significant, so this is generally not used in practice.

Re:On the Worst-Case Complexity of TimSort by jwilk on Friday August 31, 2018 @10:31AM (#17885885)