quicksort-102-sp10

quicksort-102-sp10 - Quicksort...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Quicksort http://math.hws.edu/TMCM/java/xSortLab / Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and all a[p+1...right] are >= a[p] 1.2. Quicksort a[left...p-1] 1.3. Quicksort a[p+1...right] 2. Terminate Source: David Matuszek Partitioning (Quicksort II) A key step in the Quicksort algorithm is partitioning the array We choose some (any) number p in the array to use as a pivot We partition the array into three parts: p numbers less than p numbers greater than or equal to p p Partitioning II Choose an array value (say, the first) to use as the pivot Starting from the left end, find the first element that is greater than or equal to the pivot Searching backward from the right end, find the first element that is less than the pivot Interchange (swap) these two elements Repeat, searching from where we left off, until done Partitioning To partition a[left...right]: 1. p = a[left]; l = left + 1; r = right; 2. while l < r, do 2.1. while l < right && a[l] < p { l = l + 1; } 2.2. while r > left && a[r] >= p { r = r 1;} 2.3. if l < r { swap a[l] and a[r] } 3. a[left] = a[r]; a[r] = p; 4. Terminate Example of partitioning choose pivot: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6 search: 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6 swap: 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6 search: 4 3 3 9 2 4 3 1 2...
View Full Document

This note was uploaded on 05/06/2010 for the course COMPUTER S 101 taught by Professor Sanaodeh during the Spring '08 term at NYU.

Page1 / 19

quicksort-102-sp10 - Quicksort...

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