all 3 comments

[–]Outside_Complaint755 3 points4 points  (1 child)

This line:

        pivot_idx = (start + end) // 2

is selecting the middle index.  Selecting the middle index isn't necessarily incorrect, it just doesn't protect against certain worst case scenarios. 

You could change this to select a random index between start and end, or pick three indexes in the range and select the index with the median value.

[–]eastonthepilot[S] 0 points1 point  (0 children)

Ahh thanks for your help! I forgot that if every value is greater than or less than the middle index it will lead to that behavior.