Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements.

Similar presentations


Presentation on theme: "Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements."— Presentation transcript:

1 Quick Sort

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

3 3 Quick Sort x < p p p ≤ x Partition FirstPart SecondPart p pivot A: Recursive call x < p p p ≤ x Sorted FirstPart Sorted SecondPart Sorted

4 4 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

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

6 6 Partition ExampleA: 48635172

7 7 A: 48635172 i=0 j=1

8 8 A: j=1 48635172 i=0 8

9 9 A: 486351726 i=0 j=2

10 10 Partition ExampleA: 4 8 635172 i=0 3 83 j=3i=1

11 11 Partition ExampleA: 43685172i=1 5 j=4

12 12 Partition ExampleA: 43685172i=1 1 j=5

13 13 Partition ExampleA: 43685172i=2 16 j=5

14 14 Partition ExampleA: 438572i=2 16 7 j=6

15 15 Partition ExampleA: 438572i=2 16 2 2 8i=3j=7

16 16 Partition ExampleA: 432678i=3 15 j=8

17 17 Partition ExampleA: 41678i=3 25 4 2 3

18 18 A: 3 678 1 5 42 x < 4 4 ≤ x pivot in correct position Partition Example

19 19 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

20 20 4 8 6 3 5 1 7 2 2 3 1 5 6 7 8 4 Quick-Sort(A, 0, 7) Partition A:

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

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

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

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

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

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

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

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

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

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

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

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


Download ppt "Quick Sort. 2 Divide: Pick any element p as the pivot, e.g, the first element Partition the remaining elements into FirstPart, which contains all elements."

Similar presentations


Ads by Google