Download presentation

Presentation is loading. Please wait.

Published byEric Bundick Modified over 3 years ago

1
Quicksort Lecture 3 Prof. Dr. Aydın Öztürk

2
Quicksort

3
Divide and Conquer

4
Partitioning Subroutine

5
Example of Partitioning

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

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 This expression achieves a maximum at either end points: q=0 or q=n-1.

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

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

43
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

44
Randomized quicksort

45
Randomized quicksort analysis

46
Calculating Expectation

53
Substitution Method

57
Calculating Expectation(Alternative approach) Example: Sort the following 10 distinct values: 5, 9, 3, 10, 11, 14, 8, 4, 17, 6 Pivot: 10 {5, 9, 3, 8, 4, 6}, 10, {11, 14, 17} Pivot: 6 {5, 3, 4}, 6, {9, 8}, 10, {11, 14, 17} Pivot: 4 {3}, 4, {5}, 6, {9, 8}, 10, {11, 14, 17}

58
Calculating Expectation(Alternative approach) Let X be the total number of comparisons performed in all calls to PARTITION. Let being the ith smallestelement of the array A. We define the set Let also

59
Calculating Expectation(Alternative approach) To determine the prob. that and are ever compared, we note that the values will initially in the same bracket and will remain in the same bracket if the number chosen for the first comparison is not between and. For example, if the comparison number is larger than, then all the values will go in a bracket to the left of the comparison number, and if it is smaller than, then they will all go in a bracket to the right. Thus all the values will remain in the same bracket until the first time that one of them chosen as a comparison value. At that point all the other values between i and j will be compared with this comparison value. If this comparison value is neither i or j then when compared with it, i will go into a left bracket and j will go into right bracket, consequently i and j will never be compared. If the comparison value of the set is either i or j then there will be a direct comparison between i and j.

60
Calculating Expectation(Alternative approach) The total number number of comparison performed by the algorithm

61
Calculating Expectation(Alternative approach) Taking expectation of both sides

62
Calculating Expectation(Alternative approach) Taking expectation of both sides

63
Calculating Expectation(Alternative approach)

64
Quicksort in practice

Similar presentations

OK

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on service oriented architecture Production and cost in the long run ppt on tv Ppt on business plan of a coffee shop Ppt on first conditional worksheet Ppt on endangered species of flora and fauna in india Ppt on group development and change Ppt on chapter 12 electricity prices Ppt on singly linked list in c Ppt on summary writing powerpoint Ppt on jawaharlal nehru in hindi language