- Published on
The Algorithm Design Manual (3rd) Exercise Solution 'E4-25'
Table of Contents
Introduction
This series is my solutions for algorithm exercises in'The Algorithm Design Manual' 3rd edition.
It's my solutions, not model answers, so if you find some mistakes or more sophisticated solutions, please post in comment area below.
Repository
Exercise 4-25
Question
Consider a given pair of different elements in an input array to be sorted, say and . What is the most number of times and might be compared with each other during an execution of quicksort?
Solution
1 time, no matter how we choose and .
Explanation
Comparison betweem and only occurs when either of the two is selected as pivot. And if one of them is selected, it's consumed as pivot and excluded the targets of swapping, so and are never compared.