T6 - Tutorial 6: Quick Sort & heap sort Quick sort Heap...

This preview shows pages 1–7. Sign up to view the full content.

Quick sort Heap sort Empirical Analysis

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Introduction Quick sort, like merge sort, is based on the divide-and-conquer paradigm. Intuitively, it operates as follows. Divide: CHOOSE A PIVOT ELEMENT in the list. PARTITION the list into two parts: one contains elements SMALLER THAN this pivot another contains elements LARGER THAN OR EQUAL TO this pivot. Note: the pivot is already in its FINAL POSITION in the sorted array after this step.
Introduction(cont.) Conquer : Recursively sort the two partitions using quick sort. Combine : Since the two partitions are sorted in place, no work is needed to combine them. The entire array is now sorted.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Partition How to choose a pivot and perform partition? Let’s say we want to sort an array A [ p::r ]. Assume that we select the last element in the array ( x = A [ r ]) as a pivot. Then we start to partition the array into two Segments.
Partition Maintain two variables i and j with initial values of p and r- 1 respectively.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
When i < j , Move i right, skipping over elements smaller than the pivot. Move j left, skipping over elements greater than the pivot. When both i and j have stopped, swap A [ i ] and A [ j ] and this pushes the large element to the right and pull the small element to the left. Repeat this process until
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/22/2008 for the course CS 105 taught by Professor Woo during the Spring '08 term at HKUST.

Page1 / 32

T6 - Tutorial 6: Quick Sort & heap sort Quick sort Heap...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online