0% found this document useful (0 votes)
3 views

Quick_Sort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Quick_Sort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Quick Sort

Invented by Hoare
• C.A.R. Tony Hoare, at age 26, invented his algorithm in 1960 while
trying to sort words for a machine translation project from Russian to
English. Says Hoare, “My first thought on how to do this was
bubblesort and, by an amazing stroke of luck, my second thought was
Quicksort.” It is hard to disagree with his overall assessment: “I have
been very lucky. What a wonderful way to start a career in
Computing, by discovering a new sorting algorithm!” [Hoa96]. Twenty
years later, he received the TuringAward for “fundamental
contributions to the definition and design of programming
languages”; in 1980, he was also knighted for services to education
and computer science.
QuickSort
The Sorting Problem
Input : array of n numbers, unsorted

Output : Same numbers, sorted in increasing order

Assume : all array entries distinct.


Exercise : extend QuickSort to handle duplicate entries
Partitioning Around a Pivot
Key Idea : partition array around a pivot element.
-‐ Pick element of
array pivot

-‐ Rearrange array so that


-‐ Left of pivot => less than pivot
-‐Right of pivot => greater than pivot < pivot > pivot

Note : puts pivot in its “righful position”.


Two Cool Facts About Partition
1. Linear O(n) time, no extra memory
[see next video]

2. Reduces problem size


Not a Inplace Implementation
Inplace Implementation
Example
Pseudo code for partition
Running time
Outline of QuickSort
• The Partition subroutine
• Correctness proof [optional]
• Choosing a good pivot
• Randomized QuickSort
• Analysis
– A Decomposition Principle
– The Key Insight
– Final Calculations
Quicksort
• Select a pivot (partitioning element) – here, the first
element
• Rearrange the list so that all the elements in the first s
positions are smaller than or equal to the pivot and all the
elements in the remaining n-s positions are larger than or
equal to the pivot (see next slide for an algorithm)
p

A[i]p A[i]p

• Exchange the pivot with the last element in the first (i.e.,
) subarray — the pivot is now in its final position
• Sort the two subarrays recursively
Algorithm
Best case
Quicksort Example
5 3 1 9 8 2 4 7
Analysis of Quicksort
• Best case: split in the middle — Θ(n log n)
• Worst case: sorted array! — Θ(n2)
• Average case: random arrays — Θ(n log n)

• Improvements:
• better pivot selection: median of three partitioning
• switch to insertion sort on small subfiles
• elimination of recursion
These combine to 20-25% improvement

• Considered the method of choice for internal sorting of large files


(n ≥ 10000)

You might also like