Computer Science 101 A Survey of Computer Science QuickSort
Divide and Conquer
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.
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.
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.
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”.
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
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.
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.
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
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
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.
Quicksort Example Original Pivot Move L Move U Swap
Quicksort Example (Cont.) Move L Move U Swap
Quicksort Example (Cont.) Move L Swap Move U Move L Move U Pivot Swap
Quicksort Example (Cont.) Move L Move U Swap Move L Pivot
Quicksort Example (Cont.) Move U Pivot Swap Pivot Move L 581 Move U 581 Swap 518
Quicksort Example (Cont.) Move L 518 Move U 518 Pivot Swap 158 Size Size Size
Quicksort Example (Cont.) 20 Pivot Move L Move U Swap Move L
Quicksort Example (Cont.) Move L Pivot Swap Pivot Move U Move U Pivot Swap
Quicksort Example (Cont.) 19 Size Size Size Finished
Student(?) sorts exam papers: