Quick Sort Quick Sort Divide Pick any element

  • Slides: 32
Download presentation
Quick Sort

Quick Sort

Quick Sort • Divide: • Pick any element p as the pivot, e. g,

Quick Sort • Divide: • Pick any element p as the pivot, e. g, the first element • Partition the remaining elements into First. Part, which contains all elements < p Second. Part, which contains all elements ≥ p • Recursively sort the First. Part and Second. Part • Combine: no work is necessary since sorting is done in place 2

Quick Sort A: p pivot Partition First. Part x<p Second. Part p Recursive call

Quick Sort A: p pivot Partition First. Part x<p Second. Part p Recursive call Sorted First. Part x<p p≤x Sorted Second. Part p p≤x Sorted 3

Quick Sort Quick-Sort(A, left, right) if left ≥ right return else middle ← Partition(A,

Quick Sort Quick-Sort(A, left, right) if left ≥ right return else middle ← Partition(A, left, right) Quick-Sort(A, left, middle– 1 ) Quick-Sort(A, middle+1, right) end if 4

Partition A: p A: p x<p p≤x 5

Partition A: p A: p x<p p≤x 5

Partition Example A: 4 8 6 3 5 1 7 2 6

Partition Example A: 4 8 6 3 5 1 7 2 6

Partition Example i=0 A: 4 8 6 3 5 1 7 2 j=1 7

Partition Example i=0 A: 4 8 6 3 5 1 7 2 j=1 7

Partition Example i=0 A: 4 8 6 3 5 1 7 2 j=1 8

Partition Example i=0 A: 4 8 6 3 5 1 7 2 j=1 8

Partition Example i=0 A: 4 8 6 3 5 1 7 2 j=2 9

Partition Example i=0 A: 4 8 6 3 5 1 7 2 j=2 9

Partition Example i=0 i=1 A: 4 8 3 6 83 5 1 7 2

Partition Example i=0 i=1 A: 4 8 3 6 83 5 1 7 2 j=3 10

Partition Example i=1 A: 4 3 6 8 5 1 7 2 j=4 11

Partition Example i=1 A: 4 3 6 8 5 1 7 2 j=4 11

Partition Example i=1 A: 4 3 6 8 5 1 7 2 j=5 12

Partition Example i=1 A: 4 3 6 8 5 1 7 2 j=5 12

Partition Example i=2 A: 4 3 61 8 5 16 7 2 j=5 13

Partition Example i=2 A: 4 3 61 8 5 16 7 2 j=5 13

Partition Example i=2 A: 4 3 1 8 5 6 7 2 j=6 14

Partition Example i=2 A: 4 3 1 8 5 6 7 2 j=6 14

Partition Example i=2 i=3 A: 4 3 1 82 5 6 7 82 j=7

Partition Example i=2 i=3 A: 4 3 1 82 5 6 7 82 j=7 15

Partition Example i=3 A: 4 3 1 2 5 6 7 8 j=8 16

Partition Example i=3 A: 4 3 1 2 5 6 7 8 j=8 16

Partition Example i=3 A: 24 3 1 24 5 6 7 8 17

Partition Example i=3 A: 24 3 1 24 5 6 7 8 17

Partition Example pivot in correct position A: 2 3 x<4 1 4 5 6

Partition Example pivot in correct position A: 2 3 x<4 1 4 5 6 7 8 4≤x 18

Partition(A, left, right) 1. x ← A[left] 2. i ← left 3. for j

Partition(A, left, right) 1. x ← A[left] 2. i ← left 3. for j ← left+1 to right 4. if A[j] < x then 5. i ← i + 1 6. swap(A[i], A[j]) 7. end if 8. end for j 9. swap(A[i], A[left]) 10. return i n = right – left +1 Time: cn for some constant c Space: constant 19

Quick-Sort(A, 0, 7) Partition A: 42 83 61 34 55 16 77 2 8

Quick-Sort(A, 0, 7) Partition A: 42 83 61 34 55 16 77 2 8 20

Quick-Sort(A, 0, 7) Quick-Sort(A, 0, 2) , partition A: 4 21 23 5 6

Quick-Sort(A, 0, 7) Quick-Sort(A, 0, 2) , partition A: 4 21 23 5 6 7 8 31 21

Quick-Sort(A, 0, 7) Quick-Sort(A, 0, 0) , base returncase 4 1 2 5 6

Quick-Sort(A, 0, 7) Quick-Sort(A, 0, 0) , base returncase 4 1 2 5 6 7 8 3 1 22

Quick-Sort(A, 0, 7) Quick-Sort(A, 1, 1) , base case 4 1 5 6 7

Quick-Sort(A, 0, 7) Quick-Sort(A, 1, 1) , base case 4 1 5 6 7 8 2 3 23

Quick-Sort(A, 0, 7) Quick-Sort(A, 2, 0, 2), return Quick-Sort(A, 1 2 3 4 5

Quick-Sort(A, 0, 7) Quick-Sort(A, 2, 0, 2), return Quick-Sort(A, 1 2 3 4 5 6 7 8 24

Quick-Sort(A, 0, 7) Quick-Sort(A, 2, 4, 2), 7) return , partition Quick-Sort(A, 1 2

Quick-Sort(A, 0, 7) Quick-Sort(A, 2, 4, 2), 7) return , partition Quick-Sort(A, 1 2 3 4 55 6 7 8 25

Quick-Sort(A, 0, 7) Quick-Sort(A, 5, 7) 1 2 , partition 3 4 5 6

Quick-Sort(A, 0, 7) Quick-Sort(A, 5, 7) 1 2 , partition 3 4 5 6 77 88 26

Quick-Sort(A, 0, 7) Quick-Sort(A, 6, 7) 1 2 , partition 3 4 5 6

Quick-Sort(A, 0, 7) Quick-Sort(A, 6, 7) 1 2 , partition 3 4 5 6 7 88 27

Quick-Sort(A, 0, 7) returncase , base Quick-Sort(A, 7, 7) 1 2 3 4 5

Quick-Sort(A, 0, 7) returncase , base Quick-Sort(A, 7, 7) 1 2 3 4 5 6 7 8 8 28

Quick-Sort(A, 0, 7) , return Quick-Sort(A, 6, 7) 1 2 3 4 5 6

Quick-Sort(A, 0, 7) , return Quick-Sort(A, 6, 7) 1 2 3 4 5 6 7 8 29

Quick-Sort(A, 0, 7) , return Quick-Sort(A, 5, 7) 1 2 3 4 5 6

Quick-Sort(A, 0, 7) , return Quick-Sort(A, 5, 7) 1 2 3 4 5 6 7 8 30

Quick-Sort(A, 0, 7) , return Quick-Sort(A, 4, 7) 1 2 3 4 5 6

Quick-Sort(A, 0, 7) , return Quick-Sort(A, 4, 7) 1 2 3 4 5 6 7 8 31

Quick-Sort(A, 0, 7) , done! Quick-Sort(A, 0, 7) 1 2 3 4 5 6

Quick-Sort(A, 0, 7) , done! Quick-Sort(A, 0, 7) 1 2 3 4 5 6 7 8 32