Presentation is loading. Please wait.

Presentation is loading. Please wait.

Quicksort Lecture 4. Quicksort Divide and Conquer.

Similar presentations


Presentation on theme: "Quicksort Lecture 4. Quicksort Divide and Conquer."— Presentation transcript:

1 Quicksort Lecture 4

2 Quicksort

3 Divide and Conquer

4 Partitioning Subroutine

5 Example of Partitioning

6

7

8

9

10

11

12

13

14

15

16

17 Running time for PARTITION The running time of PARTITION on the subarray A[p...r] is where n=r-p+1

18 Pseudo code for Quicksort

19 Analysis of Quicksort

20 Worst-case of quicksort

21 Worst-case decision tree

22

23

24

25

26

27 Worst-case analysis For the worst case, we can write the recurrence equation as We guess that

28 Worst-case analysis This expression achieves a maximum at either end points: q=0 or q=n-1. Using the maximum of T(n) we have Thus

29 Worst-case analysis We can also show that the recurrence equation as Has a solution of We guess that

30 Worst-case analysis Using the maximum of T(n) we have We can pick the constant c 1 large enough so that and Thus the worst case running time of quicksort is

31 Best-case analysis

32 Analysis of almost best-case

33

34

35

36

37 Best-case analysis For the best case, we can write the recurrence equation as We guess that

38 Best-case analysis

39 This expression achieves a minimum at Using the minimum of T(n) we have

40 More intuition

41 Randomized quicksort

42 Standard Problematic Algorithm :

43 Randomized quicksort R ANDOMIZED-PARTITION (A, p, r) 1i←RANDOM(p, r) 2exchange A[r]↔A[i] 3return PARTITION(A, p, r) RANDOMIZED-QUICKSORT (A,p,r) 1if p<r 2then q← R ANDOMIZED-PARTITION (A, p, r) RANDOMIZED-QUICKSORT (A, p, q-1) RANDOMIZED-QUICKSORT (A, q+1, r)

44 Randomized quicksort

45 Randomized quicksort analysis

46 Calculating Expectation

47

48

49

50

51

52

53 Substitution Method

54

55

56

57 Quicksort in practice


Download ppt "Quicksort Lecture 4. Quicksort Divide and Conquer."

Similar presentations


Ads by Google