Quicksort (details)

 

CS 241: Data Structures
Lecture #5: Sorting algorithms

Quicksort (details)
bullet Choosing the pivot the preferred method of choosing a pivot is the median of three approach, which takes the median value between the first, last and middle values in the array

this approach has the advantage that it is fairly quick and simple, and does not degrade when the input is sorted or anti-sorted

Control bar


















































 

CS 241: Data Structures
Lecture #5: Sorting algorithms

Quicksort (details)
bullet Choosing the pivot
bullet Partitioning: burning the candle at both ends in order to partition the values in an array, we can move two counters from the ends of the array toward the middle, swapping values when we find items which are too large on the left and too small on the right (relative to the pivot value)

Control bar


















































 

CS 241: Data Structures
Lecture #5: Sorting algorithms

Quicksort (details)
bullet Choosing the pivot
bullet Partitioning: burning the candle at both ends
bullet Keys equal to the pivot by symmetrically allowing equal values to be swapped, we can actually help achieve a more balanced partition when there are many pivot-equivalent values

Control bar


















































 

CS 241: Data Structures
Lecture #5: Sorting algorithms

Quicksort (details)
bullet Choosing the pivot
bullet Partitioning: burning the candle at both ends
bullet Keys equal to the pivot
bullet Optimizing for small arrays there is some extra overhead expense to recursive calls; studies show that cutting off the recursion and using (e.g.) insertion sort for arrays of size roughly 10 improves performance

Control bar