23
Computer Science 101 A Survey of Computer Science QuickSort

Computer Science 101 A Survey of Computer Science QuickSort

Embed Size (px)

Citation preview

Page 1: Computer Science 101 A Survey of Computer Science QuickSort

Computer Science 101A Survey of Computer Science

QuickSort

Page 2: Computer Science 101 A Survey of Computer Science QuickSort

Divide and Conquer

Page 3: 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.

Page 4: Computer Science 101 A Survey of Computer Science QuickSort

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.

Page 5: Computer Science 101 A Survey of Computer Science QuickSort

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.

Page 6: Computer Science 101 A Survey of Computer Science QuickSort

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”.

Page 7: Computer Science 101 A Survey of Computer Science QuickSort

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

Page 8: Computer Science 101 A Survey of Computer Science QuickSort

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.

Page 9: Computer Science 101 A Survey of Computer Science QuickSort

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.

Page 10: Computer Science 101 A Survey of Computer Science QuickSort

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

Page 11: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Partition Algorithm Exchange the median of the first 3

elements with the first elementSet P to first position of listSet L to second position of listSet U to last position of listWhile L ≤ U do

While N(L) N(P) doSet L to L+1

end-of-loopWhile N(U) > N(P) do

Set U to U-1end-of-loopIf L < U then

Exchange N(L) and N(U)end-of-loopExchange N(P) and N(U)

Left markercharges to right

Right markercharges to left

Page 12: Computer Science 101 A Survey of Computer Science QuickSort

QuickSort - The Algorithm

If the list to sort has more than 1 element thenIf the list has exactly two elements then If the elements are out of order thenExchange themelse 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.

Page 13: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example

15 8 19 5 30 20 10 1 28 25 12

19 8 15 5 30 20 10 1 28 25 12Original

19 8 15 5 30 20 10 1 28 25 12Pivot

15 8 19 5 30 20 10 1 28 25 12Move L

15 8 19 5 30 20 10 1 28 25 12Move U

15 8 12 5 30 20 10 1 28 25 19Swap

Page 14: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example (Cont.)

15 8 12 5 30 20 10 1 28 25 19Move L

15 8 12 5 30 20 10 1 28 25 19

15 8 12 5 30 20 10 1 28 25 19Move U

15 8 12 5 30 20 10 1 28 25 19

15 8 12 5 30 20 10 1 28 25 19

15 8 12 5 1 20 10 30 28 25 19Swap

Page 15: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example (Cont.)15 8 12 5 1 20 10 30 28 25 19Move L

15 8 12 5 1 10 20 30 28 25 19Swap

15 8 12 5 1 20 10 30 28 25 19Move U

15 8 12 5 1 10 20 30 28 25 19Move L

15 8 12 5 1 10 20 30 28 25 19Move U

10 8 12 5 1 15 20 30 28 25 19PivotSwap

Page 16: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example (Cont.)

10 8 12 5 1Move L

10 8 12 5 1

10 8 12 5 1Move U

10 8 1 5 12Swap

10 8 1 5 12Move L

10 8 1 5 12

Pivot 10 8 12 5 1 20 30 28 25 1915

Page 17: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example (Cont.)10 8 1 5 12Move U

5 8 1 10 12PivotSwap

Pivot 5 8 1 10 12 20 30 28 25 1915

Move L 5 8 1

Move U 5 8 1

Swap 5 1 8

Page 18: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example (Cont.)

Move L 5 1 8

Move U 5 1 8

PivotSwap 1 5 8

Size 1 11 5 8 10 12 20 30 28 25 1915

Size 1 1 5 8 10 12 20 30 28 25 1915

Size 1 11 5 8 10 12 20 30 28 25 1915

Page 19: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example (Cont.)20Pivot 11 5 108 12 30 28 25 1915

28Move L 30 20 25 19

28Move U 30 20 25 19

28Swap 19 20 25 30

Move L 28 19 20 25 30

28 19 20 25 30

28 19 20 25 30

Page 20: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example (Cont.)

Move L 20 19 25

PivotSwap 25 19 20 28 30

25Pivot 11 85 10 12 19 20 28 3015

Move U 28 19 20 25 30

20 19 25

Move U 20 19 25

PivotSwap 19 20 25

Page 21: Computer Science 101 A Survey of Computer Science QuickSort

Quicksort Example (Cont.)

19Size 1 11 85 10 12 20 25 28 3015

19Size 1 11 85 10 12 20 25 28 3015

19Size 1 11 85 10 12 20 25 28 3015

19Finished 11 85 10 12 20 25 28 3015

Page 22: Computer Science 101 A Survey of Computer Science QuickSort

Student(?) sorts exam papers:

Page 23: Computer Science 101 A Survey of Computer Science QuickSort