Sorting Chapter 7 1 Quick Sort One of the most popular fast - - PowerPoint PPT Presentation

sorting
SMART_READER_LITE
LIVE PREVIEW

Sorting Chapter 7 1 Quick Sort One of the most popular fast - - PowerPoint PPT Presentation

Sorting Chapter 7 1 Quick Sort One of the most popular fast sorting algorithms Quick sort overcomes the drawback of merge sort of creating an additional array Generally, quick sort is the most efficient algorithm for large arrays 2 Quick


slide-1
SLIDE 1

Sorting

Chapter 7

1

slide-2
SLIDE 2

Quick Sort

One of the most popular fast sorting algorithms Quick sort overcomes the drawback of merge sort of creating an additional array Generally, quick sort is the most efficient algorithm for large arrays

2

slide-3
SLIDE 3

Quick Sort

Pick any element in the array (call it the pivot) Place the pivot in its correct position in the array Move all smaller elements to the left Move all bigger elements to the right Sort the left and right sides recursively

3

slide-4
SLIDE 4

Quick Sort Example

4

8 10 5 1 3 20 13 7 2 12

slide-5
SLIDE 5

Quick Sort Example

5

8 10 5 1 3 20 13 7 2 12

pivot

Select the pivot

slide-6
SLIDE 6

Quick Sort Example

6

8 10 5 1 3 20 13 7 2 12

pivot

Partition the array

slide-7
SLIDE 7

Quick Sort Example

7

Recursively sort both sides 10 5 1 3 20 13 7 2 12 8

pivot Quick sort Quick sort

slide-8
SLIDE 8

Selecting the Pivot

What would be a good pivot? What would be a bad pivot? What is the ideal pivot? Naïve selection: First element in the list A good selection: A random element A better selection: Median-of-three

8

slide-9
SLIDE 9

Median-of-three

9

8 10 5 1 3 20 13 7 2 12 pivot 46 15 10 6 9 15 2 5 18 12 pivot 15 48 29 18 1 19 33 23 27 41 pivot

slide-10
SLIDE 10

Partitioning

The key idea about Quick Sort is to make an in-place partitioning Take the pivot out of the way Move bigger elements to the right Move smaller elements to the left Replace the pivot at its place

10

slide-11
SLIDE 11

Partitioning Example

11

8 10 5 1 11 20 13 7 22 12 pivot

slide-12
SLIDE 12

Partitioning Example

12

8 10 15 1 12 20 13 7 22 11 pivot

slide-13
SLIDE 13

Partitioning Example

13

8 10 15 1 12 20 13 7 22 11 pivot i j

slide-14
SLIDE 14

Partitioning Example

14

8 10 15 1 12 20 13 7 22 11 pivot

i

j

slide-15
SLIDE 15

Partitioning Example

15

8 10 15 1 12 20 13 7 22 11 pivot

i

j

slide-16
SLIDE 16

Partitioning Example

16

8 10 15 1 12 20 13 7 22 11 pivot

i

j

slide-17
SLIDE 17

Partitioning Example

17

8 10 15 1 12 20 13 7 22 11 pivot i

j

slide-18
SLIDE 18

Partitioning Example

18

8 10 15 1 12 20 13 7 22 11 pivot i

j

slide-19
SLIDE 19

Partitioning Example

19

8 10 15 1 12 20 13 7 22 11 pivot i j

slide-20
SLIDE 20

Partitioning Example

20

8 10 7 1 12 20 13 15 22 11 pivot i j

slide-21
SLIDE 21

Partitioning Example

21

8 10 7 1 12 20 13 15 22 11 pivot i j

slide-22
SLIDE 22

Partitioning Example

22

8 10 7 1 12 20 13 15 22 11 pivot

i

j

slide-23
SLIDE 23

Partitioning Example

23

8 10 7 1 12 20 13 15 22 11 pivot

i

j

slide-24
SLIDE 24

Partitioning Example

24

8 10 7 1 12 20 13 15 22 11 pivot i

j

slide-25
SLIDE 25

Partitioning Example

25

8 10 7 1 12 20 13 15 22 11 pivot i

j

slide-26
SLIDE 26

Partitioning Example

26

8 10 7 1 12 20 13 15 22 11 pivot i j

slide-27
SLIDE 27

Partitioning Example

27

8 10 7 1 12 20 13 15 22 11 pivot i

j

slide-28
SLIDE 28

Partitioning Example

28

8 10 7 1 12 20 13 15 22 11 pivot i j

i and j are reversed!

slide-29
SLIDE 29

Partitioning Example

29

8 10 7 1 11 20 13 15 22 12 pivot i j

slide-30
SLIDE 30

Analysis of Quick Sort

Cost of the partitioning step

O(n): One scan over the list

Worst case

The sizes of the two sublists are 0 and n-1 O(n2)

Best case

The sizes of the two sublists are almost equal O(n log n)

Average case

None of the lists is excessively large or small O(n log n)

30

slide-31
SLIDE 31

Comparison of Sorting Algorithms

Algorithm Worst- case Best- case Average Stable* In-place Insertion Selection Bubble Shell Heap Merge Quick

31

*A sorting algorithm is said to be stable if equal items remain in the same

  • rder after sorting
slide-32
SLIDE 32

Comparison of Sorting Algorithms

Algorithm Worst- case Best- case Average Stable* In-place Insertion O(n2) O(n) O(n2)   Selection O(n2) O(n2) O(n2)  Bubble O(n2) O(n) O(n2)   Shell O(n3/2)  Heap O(n log n) O(n log n) O(n log n)  Merge O(n log n) O(n log n) O(n log n)  Quick O(n2)** O(n log n) O(n log n) 

32

*A sorting algorithm is said to be stable if equal items remain in the same

  • rder after sorting

** Can be reduced to O(n log n) with a smart pivot selection algorithm

slide-33
SLIDE 33

Lower Bound for Sorting

33

Can we create a sorting algorithm with an asymptotic running time that is lower than O(n log n) in the worst case? Assumptions

Array elements can be in any order Only comparisons are used to sort elements

slide-34
SLIDE 34

Decision Tree (Execution Tree)

34

Comparison Initial state Execution terminated Number of comparisons

slide-35
SLIDE 35

Worst-case

Deepest part of the tree Number of leaf nodes

Equal to total number of permutations n!

Height of the tree

log(n!)

log 𝑜! = Ω 𝑜 log 𝑜 𝑜 log 𝑜 is a lower bound for any comparison- based sorting algorithm

35