Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 101 A Survey of Computer Science QuickSort.

Similar presentations


Presentation on theme: "Computer Science 101 A Survey of Computer Science QuickSort."— Presentation transcript:

1 Computer Science 101 A Survey of Computer Science QuickSort

2 Divide and Conquer

3 Divide and Conquer is a common strategy used in computer science. The idea is that for a given problem, we try to break it into smaller problems (perhaps of the same type and then solve the smaller problems) Of course, we must consider how to solve the smaller problems.

4 Sorting -Quicksort Strategy - Divide and Conquer: –Partition list with small elements in first part and large elements in second part –Sort the first part. –Sort the second part.

5 Quicksort (cont.) Question - How do we sort the sections? Answer - Apply Quicksort to them. Recursive algorithm - one which makes use of itself to solve smaller problems of the same type.

6 Quicksort (cont.) Question - Will this recursive process ever stop? Answer - Yes, when the problem is “small enough”, we no longer use recursion. Such cases are called “anchor cases”.

7 Recursion Example The factorial function could be defined this way: n! = { 1 if n=1 { n ((n-1)!) otherwise Example: 4! = 4 x 3! = 4 x 3 x 2! = 4 x 3 x 2 x 1! = 4 x 3 x 2 x 1 Smaller problem of same type Anchor case

8 Quicksort - Partitioning To partition, we choose a “pivot element” from the list. The elements which are less than or equal to the pivot go into the first section. The elements larger than the pivot go into the second section.

9 Quicksort - The Pivot Ideal would be to choose the median as the pivot, but this would take too long. Some programs just choose the first element. Our choice - choose the median of the first three elements.

10 Quicksort Partition Variables: N(I),N(I+1), …, N(K) - list to partition P - position of the pivot element L - Right hand marker for the first section U - Left hand marker for the second section

11 Quicksort Partition Algorithm Exchange the median of the first 3 elements with the first element Set P to first position of list Set L to second position of list Set U to last position of list While L ≤ U do While N(L)  N(P) do Set L to L+1 end-of-loop While N(U) > N(P) do Set U to U-1 end-of-loop If L < U then Exchange N(L) and N(U) end-of-loop Exchange N(P) and N(U) Left marker charges to right Right marker charges to left

12 QuickSort - The Algorithm If the list to sort has more than 1 element then If the list has exactly two elements then If the elements are out of order then Exchange them else Perform the Partition Algorithm on list Apply QuickSort to the first section Apply QuickSort to the second section Note: Anchor cases are when the list has 1 or 2 elements – recursion is used for 3 or more.

13 Quicksort Example 15819530201012825121981553020101282512 Original 1981553020101282512 Pivot 1581953020101282512 Move L 1581953020101282512 Move U 1581253020101282519 Swap

14 Quicksort Example (Cont.) 1581253020101282519 Move L 15812530201012825191581253020101282519 Move U 158125302010128251915812530201012825191581251201030282519 Swap

15 Quicksort Example (Cont.) 1581251201030282519 Move L 1581251102030282519 Swap 1581251201030282519 Move U 1581251102030282519 Move L 1581251102030282519 Move U 1081251152030282519 Pivot Swap

16 Quicksort Example (Cont.) 1081251 Move L 10812511081251 Move U 1081512 Swap 1081512 Move L 1081512 Pivot 1081251203028251915

17 Quicksort Example (Cont.) 1081512 Move U 5811012 Pivot Swap Pivot 5811012203028251915 Move L 581 Move U 581 Swap 518

18 Quicksort Example (Cont.) Move L 518 Move U 518 Pivot Swap 158 Size 11581012203028251915 Size 1 1581012203028251915 Size 11581012203028251915

19 Quicksort Example (Cont.) 20 Pivot1510812302825191528 Move L 3020251928 Move U 3020251928 Swap 19202530 Move L 281920253028192025302819202530

20 Quicksort Example (Cont.) Move L 201925 Pivot Swap 2519202830 25 Pivot18510121920283015 Move U 2819202530201925 Move U 201925 Pivot Swap 192025

21 Quicksort Example (Cont.) 19 Size 11851012202528301519 Size 11851012202528301519 Size 11851012202528301519 Finished18510122025283015

22 Student(?) sorts exam papers:

23


Download ppt "Computer Science 101 A Survey of Computer Science QuickSort."

Similar presentations


Ads by Google