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

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

View Full Document Right Arrow Icon
Quick sort Heap sort Empirical Analysis
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
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.
Background image of page 4
Partition Maintain two variables i and j with initial values of p and r- 1 respectively.
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Image of page 7
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 Right Arrow Icon
Ask a homework question - tutors are online