QuickSort

Set pivot to partition array pivot return up 7

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: der items r[up] out-ofr[lb] r[lb] ← r[up] r[up] r[up] ← pivot // set pivot to partition array pivot return up 7 quicksort: quicksort: example notes 3 1 6 3 2 5 4 down 3 up 1 6 3 2 5 4 down down 3 5 5 r[down] > 3 r[up] <= 3 up 1 2 3 6 5 4 5 5 4 5 down down up 3 pivot pivot = 3 1 2 3 6 r[down] > 3 r[up] <= 3 up < down end partition end 8 quicksort: quicksort: example notes 3 1 down 3 1 2 3 6 5 4 5 qsort qsort (0, 2) pivot = 3 3 6 5 4 5 down = ub r[up] <= 3 up 2 up, down 2 2 1 1 3 3 6 5 4 5 end partition 3 3 6 5 4 5 qsort (0, 1) 9 quicksort: quicksort: example notes 2 1 3 3 6 5 4 5 pivot = 2 3 3 6 5 4 5 down = ub r[up] <= 2 down up 2 1 up, down 1 1 2 3 3 6 5 4 5 end partition 2 3 3 6 5 4 5 qsort (0, 0) 10 quicksort: quicksort: example 1 notes ub = lb return 2 1 1 3 6 5 4 5 2 1 3 3 3 6 5 4 5 qsort (2, 1) end qsort(0, 1) 3 3 6 5 4 5 qsort (2, 2) end qsort(0, 2) 3 6 5 4 5 qsort (4, 7) 2 2 3 11 quicksort: quicksort: example notes 1 2 3 3 6 5 4 down 1 2 3 3 6 5 pivot = 6 up 5 4 5 down = ub r[up] <= 6 up, down 1 2 3 3 5 5 4 1 2 3 3 5 5 4 6 6 end partition qsort qsort (4, 6) 12 quicksort: quicksort: example notes 1 2 3 3 5 5 down 1 2 3 3 5 4 6 pivot = 5 6 down = ub up = ub up 5 4 up, up, down 1 2 3 3 4 5 1 2 3 3 4 5 5 5 6 6 end partition qsort (4, 5) 13 quicksort: quicksort: example notes 1 2 3 3 4 5 5 6 pivot = 4 5 6 down = ub down up 1 2 3 3 4 5 up down 1 2 3 3 4 5 5 6 qsort (4, 3) return 1 2 3 3 4 5 5 6 qsort qsort (5, 5) 14 return quicksort: quicksort: example notes 1 2 3 3 4 5 1 2 3 3 4 5 5 1 2 3 3 4 5 5 6 qsort(8, 7) end qsort(4, 7) 4 5 5 6 end end 1 2 3 3 5 6 6 end qsort(4, 5) qsort(7, 6) end qsort(4, 6) 15 Quicksort comments ! Due Due to partitioning, quicksort is not stable ! Recursion Recursion has overhead of time and memory memory ! Important Important that implementation creates no infinite infinite recursions...
View Full Document

Ask a homework question - tutors are online