Quicksort with swapping middle and first element in each partition -
assume have standard, two-way partition quicksort algorithm pivots on first element. however, in slight variant of quicksort, first swap first , middle elements, , pivot on 'new' first element. question is, change worst-case running time?
my initial thinking no, in each sub-array elements still in random order relative each other, , switching first , middle elements not change overall runtime. interested in finding worst-case scenario, i'm not sure if there's 'special' array cause slight variant change worst-case runtime of original algorithm.
quicksort’s worst case when pivot minimum or maximum. in mind, can build worst-case array:
- [1, 2] (every element in two-element array min or max)
- [3, 1, 2] post-swap produces above
- [1, 3, 2] pre-swap
- [4, 1, 3, 2] post-swap produces above
- [1, 4, 3, 2] pre-swap
- [5, 1, 4, 3, 2] post-swap produces above
- [4, 1, 5, 3, 2] pre-swap
- [6, 4, 1, 5, 3, 2] post-swap produces above
- [1, 4, 6, 5, 3, 2] pre-swap
- etc.
Comments
Post a Comment