Data Structures in Java Lecture 15: Sorting II 11/11/2015 Daniel - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Lecture 15: Sorting II 11/11/2015 Daniel - - PowerPoint PPT Presentation

Data Structures in Java Lecture 15: Sorting II 11/11/2015 Daniel Bauer 1 Quick Sort Another divide-and-conquer algorithm. Pick any pivot element v. Partition the array into elements x v and x v. Recursively sort the


slide-1
SLIDE 1

Data Structures in Java

Lecture 15: Sorting II

11/11/2015 Daniel Bauer

1

slide-2
SLIDE 2
  • Another divide-and-conquer algorithm.
  • Pick any pivot element v.
  • Partition the array into elements
  • x ≤ v and x ≥ v.
  • Recursively sort the partitions, then concatenate them.

Quick Sort

51 32 21 1 34 8 64 2

2

slide-3
SLIDE 3
  • Another divide-and-conquer algorithm.
  • Pick any pivot element v.
  • Partition the array into elements
  • x ≤ v and x ≥ v.
  • Recursively sort the partitions, then concatenate them.

Quick Sort

51 32 21 1 34 8 64 2 21

v

2

slide-4
SLIDE 4
  • Another divide-and-conquer algorithm.
  • Pick any pivot element v.
  • Partition the array into elements
  • x ≤ v and x ≥ v.
  • Recursively sort the partitions, then concatenate them.

Quick Sort

51 32 21 1 34 8 64 2 21

v

1 8 2

x ≤ v

2

slide-5
SLIDE 5
  • Another divide-and-conquer algorithm.
  • Pick any pivot element v.
  • Partition the array into elements
  • x ≤ v and x ≥ v.
  • Recursively sort the partitions, then concatenate them.

Quick Sort

51 32 21 1 34 8 64 2 21

v

1 8 2

x ≤ v

51 32 34 64

x ≥ v

2

slide-6
SLIDE 6
  • Another divide-and-conquer algorithm.
  • Pick any pivot element v.
  • Partition the array into elements
  • x ≤ v and x ≥ v.
  • Recursively sort the partitions, then concatenate them.

Quick Sort

51 32 21 1 34 8 64 2 21

v

8 1 2

x ≤ v

51 64 32 34

x ≥ v

3

slide-7
SLIDE 7
  • Another divide-and-conquer algorithm.
  • Pick any pivot element v.
  • Partition the array into elements
  • x ≤ v and x ≥ v.
  • Recursively sort the partitions, then concatenate them.

Quick Sort

51 32 21 1 34 8 64 2 21

v

8 1 2

x ≤ v

51 64 32 34

x ≥ v

3

slide-8
SLIDE 8

Quick Sort

51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64

4

slide-9
SLIDE 9

Quick Sort

51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 32 34 64

5

slide-10
SLIDE 10

Quick Sort

51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 32 34 64 32 34

6

slide-11
SLIDE 11

Quick Sort

51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 32 34 64 32 34

6

slide-12
SLIDE 12

Quick Sort

51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 64 32 34

7

slide-13
SLIDE 13

Quick Sort

51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 64 32 34

7

slide-14
SLIDE 14

Quick Sort

51 32 21 1 34 8 64 2 21 1 8 2 51 32 34 64 1 8 2 51 64 32 34

7

slide-15
SLIDE 15

51 32 21 1 34 8 64 2 21 8 1 2 51 64 32 34

Quick Sort

8

slide-16
SLIDE 16

51 32 21 1 34 8 64 2 21 8 1 2 51 64 32 34

Quick Sort

8

slide-17
SLIDE 17

Quick Sort

32 34 51 64 1 2 8 21

  • How do we partition the array efficiently (in place)?
  • How do we pick a pivot element?
  • Running time performance on quick sort 


depends on our choice.

  • Bad choice leads to running time.

9

slide-18
SLIDE 18

Partitioning the Array

  • We don’t want to use any extra space. Need to

partition the array in place.

  • Use swaps to push all elements x ≤ v to the left and

elements x ≥ v to the right.

51 32 21 1 34 8 64 2

10

slide-19
SLIDE 19

Partitioning the Array

  • We don’t want to use any extra space. Need to

partition the array in place.

  • Use swaps to push all elements x ≤ v to the left and

elements x ≥ v to the right.

51 32 21 1 34 8 64 2

Move the pivot to the end.

10

slide-20
SLIDE 20

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:

11

slide-21
SLIDE 21

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:

11

slide-22
SLIDE 22

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:

11

slide-23
SLIDE 23

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:

11

slide-24
SLIDE 24

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:

11

slide-25
SLIDE 25

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:

11

slide-26
SLIDE 26

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:

11

slide-27
SLIDE 27

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:
  • Swap array[i] with v.
  • i points to a value greater than the pivot.

11

slide-28
SLIDE 28

Partitioning the Array

51 1 21 32 34 8 64 2

i j

  • Move i right until we find an element array[i] ≥ v
  • Move j left until we find an element array[j] ≤ v.
  • if i ≥ j break
  • Swap array[i] and array[j].
  • While True:
  • Swap array[i] with v.
  • i points to a value greater than the pivot.

11

slide-29
SLIDE 29

Partitioning the Array

¡ ¡ ¡ ¡public ¡static ¡void ¡quicksort(Integer[] ¡a, ¡int ¡left, ¡int ¡right) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(right ¡> ¡left) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡v ¡= ¡find_pivot_index(a, ¡left, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡i ¡= ¡0; ¡ ¡ ¡int ¡j ¡= ¡right-­‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡move ¡pivot ¡to ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡tmp ¡= ¡a[v]; ¡a[v] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡{ ¡// ¡partition ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++i] ¡< ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++j] ¡> ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(i ¡>= ¡j) ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[j]; ¡a[j] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡move ¡pivot ¡back ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡//recursively ¡sort ¡both ¡partitions ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡quicksort(a,left, ¡i-­‑1); ¡ ¡quicksort(a,i+1, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡}

12

slide-30
SLIDE 30

Partitioning the Array

¡ ¡ ¡ ¡public ¡static ¡void ¡quicksort(Integer[] ¡a, ¡int ¡left, ¡int ¡right) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(right ¡> ¡left) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡v ¡= ¡find_pivot_index(a, ¡left, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡i ¡= ¡0; ¡ ¡ ¡int ¡j ¡= ¡right-­‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡move ¡pivot ¡to ¡the ¡end ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡tmp ¡= ¡a[v]; ¡a[v] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(true) ¡{ ¡// ¡partition ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++i] ¡< ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(a[++j] ¡> ¡v) ¡{}; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(i ¡>= ¡j) ¡break; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[j]; ¡a[j] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡move ¡pivot ¡back ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[i]; ¡a[i] ¡= ¡a[right]; ¡a[right] ¡= ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡//recursively ¡sort ¡both ¡partitions ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡quicksort(a,left, ¡i-­‑1); ¡ ¡quicksort(a,i+1, ¡right); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡}

O(N)

12

slide-31
SLIDE 31

Quick Sort: Worst Case

  • Running time depends on the how the pivot

partitions the array.

  • Worst case: Pivot is always the smallest or largest
  • element. One of the partitions is empty!

51 32 21 1 34 8 64 2

.
 .
 . 13

slide-32
SLIDE 32

Quick Sort: Worst Case

  • Running time depends on the how the pivot

partitions the array.

  • Worst case: Pivot is always the smallest or largest
  • element. One of the partitions is empty!

51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1

.
 .
 . 13

slide-33
SLIDE 33

Quick Sort: Worst Case

  • Running time depends on the how the pivot

partitions the array.

  • Worst case: Pivot is always the smallest or largest
  • element. One of the partitions is empty!

51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64

.
 .
 . 13

slide-34
SLIDE 34

Quick Sort: Worst Case

51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64

51 64 51 64

T(1) = 1

14

slide-35
SLIDE 35

Quick Sort: Worst Case

51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64

51 64 51 64

T(1) = 1 T(2) = T(1) + 2

Time for partitioning

14

slide-36
SLIDE 36

Quick Sort: Worst Case

51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64

51 64 51 64

T(1) = 1 T(2) = T(1) + 2

Time for partitioning

T(N-2) = T(N-3) + (N-2)

14

slide-37
SLIDE 37

Quick Sort: Worst Case

51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64

51 64 51 64

T(1) = 1 T(2) = T(1) + 2

Time for partitioning

T(N-1) = T(N-2) + (N-1) T(N-2) = T(N-3) + (N-2)

14

slide-38
SLIDE 38

Quick Sort: Worst Case

51 32 21 1 34 8 64 2 51 32 21 34 8 64 2 1 2 51 32 21 34 8 64

51 64 51 64

T(1) = 1 T(2) = T(1) + 2 T(N) = T(N-1) + N

Time for partitioning

T(N-1) = T(N-2) + (N-1) T(N-2) = T(N-3) + (N-2)

14

slide-39
SLIDE 39

Quick Sort: Worst Case

15

slide-40
SLIDE 40

Quick Sort: Worst Case

15

slide-41
SLIDE 41

Quick Sort: Worst Case

15

slide-42
SLIDE 42

Quick Sort: Worst Case

15

slide-43
SLIDE 43

Quick Sort: Worst Case

15

slide-44
SLIDE 44

Quick Sort: Best Case

  • Best case: Pivot is always the median element. 


Both partitions have about the same size.

51 32 21 1 34 8 64 2

16

slide-45
SLIDE 45

Quick Sort: Best Case

  • Best case: Pivot is always the median element. 


Both partitions have about the same size.

51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2

16

slide-46
SLIDE 46

Quick Sort: Best Case

  • Best case: Pivot is always the median element. 


Both partitions have about the same size.

51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2

16

slide-47
SLIDE 47

Quick Sort: Best Case

  • Best case: Pivot is always the median element. 


Both partitions have about the same size.

51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2 51 32 34 64

16

slide-48
SLIDE 48

Quick Sort: Best Case

  • Best case: Pivot is always the median element. 


Both partitions have about the same size.

51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2 51 32 34 64

T(N) = 2 T(N/2) +N (we ignore the pivot element, so this overestimates the running time slightly)

17

slide-49
SLIDE 49

Quick Sort: Best Case

  • Best case: Pivot is always the median element. 


Both partitions have about the same size.

51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2 51 32 34 64

T(N) = 2 T(N/2) +N T(N/2) = 2 T(N/4) +N/2 (we ignore the pivot element, so this overestimates the running time slightly)

17

slide-50
SLIDE 50

Quick Sort: Best Case

  • Best case: Pivot is always the median element. 


Both partitions have about the same size.

51 32 21 1 34 8 64 2 51 32 21 1 34 8 64 2 1 8 2 51 32 34 64

T(N) = 2 T(N/2) +N T(N/2) = 2 T(N/4) +N/2 T(1) = 1

(we ignore the pivot element, so this overestimates the running time slightly)

17

slide-51
SLIDE 51

Quick Sort: Best Case

(note that this is the same analysis as for Merge Sort)

18

slide-52
SLIDE 52

Quick Sort: Best Case

(note that this is the same analysis as for Merge Sort)

18

slide-53
SLIDE 53

Quick Sort: Best Case

(note that this is the same analysis as for Merge Sort)

18

slide-54
SLIDE 54

Quick Sort: Best Case

assume

(note that this is the same analysis as for Merge Sort)

18

slide-55
SLIDE 55

Quick Sort: Best Case

assume

(note that this is the same analysis as for Merge Sort)

18

slide-56
SLIDE 56

Quick Sort: Best Case

assume

(note that this is the same analysis as for Merge Sort)

18

slide-57
SLIDE 57

Choosing the Pivot

19

slide-58
SLIDE 58

Choosing the Pivot

  • Ideally we want to choose the median in each

partition, but we don’t know where it is!

19

slide-59
SLIDE 59

Choosing the Pivot

  • Ideally we want to choose the median in each

partition, but we don’t know where it is!

  • Computing the pivot should be a constant time
  • peration.

19

slide-60
SLIDE 60

Choosing the Pivot

  • Ideally we want to choose the median in each

partition, but we don’t know where it is!

  • Computing the pivot should be a constant time
  • peration.
  • Choosing the element at the beginning/end/middle

is a terrible idea! 
 Better: Choose a random element.

19

slide-61
SLIDE 61

Choosing the Pivot

  • Ideally we want to choose the median in each

partition, but we don’t know where it is!

  • Computing the pivot should be a constant time
  • peration.
  • Choosing the element at the beginning/end/middle

is a terrible idea! Better: Choose a random element.

  • Good approximation for median: “Median-of-three”

19

slide-62
SLIDE 62

Choosing a Pivot: 
 Median of Three

51 32 21 1 34 8 64 2

Choose the median of array[0], array[n]m and array[n/2].

20

slide-63
SLIDE 63

Choosing a Pivot: 
 Median of Three

51 32 21 1 34 8 64 2 2 1 51 32 21 34 8 64

Choose the median of array[0], array[n]m and array[n/2].

20

slide-64
SLIDE 64

Choosing a Pivot: 
 Median of Three

51 32 21 1 34 8 64 2 2 1 51 32 21 34 8 64 51 32 21 34 8 64

Choose the median of array[0], array[n]m and array[n/2].

20

slide-65
SLIDE 65

Choosing a Pivot: 
 Median of Three

51 32 21 1 34 8 64 2 2 1 51 32 21 34 8 64 51 32 21 34 8 64

Choose the median of array[0], array[n]m and array[n/2].

21 8 32

20

slide-66
SLIDE 66

Median of Three

¡ ¡ ¡ ¡public ¡static ¡int ¡find_pivot_index(Integer[] ¡a, ¡int ¡left, ¡int ¡right) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡center ¡= ¡( ¡left ¡+ ¡right ¡) ¡/ ¡2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡tmp; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[center] ¡< ¡a[left]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[center]; ¡a[center] ¡= ¡a[left]; ¡a[left] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[right] ¡< ¡a[left]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[right]; ¡a[right] ¡= ¡a[left]; ¡a[left] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(a[right] ¡< ¡a[center]) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmp ¡= ¡a[right]; ¡a[right] ¡= ¡a[center]; ¡a[center] ¡= ¡tmp;} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡center; ¡ ¡ ¡ ¡ ¡}

21

slide-67
SLIDE 67

Analyzing Quick Sort

  • Worst case running time: 

  • Best and average case (random pivot): 

  • Is QuickSort stable?


  • Space requirement? 


22

slide-68
SLIDE 68

Analyzing Quick Sort

  • Worst case running time: 

  • Best and average case (random pivot): 

  • Is QuickSort stable?


  • Space requirement? 


  • No. Partitioning can change order of elements.


(but can make QuickSort stable).

22

slide-69
SLIDE 69

Analyzing Quick Sort

  • Worst case running time: 

  • Best and average case (random pivot): 

  • Is QuickSort stable?


  • Space requirement? 


  • No. Partitioning can change order of elements.


(but can make QuickSort stable). In-place O(1), but the method activation stack grows with the running time. O(N)

22

slide-70
SLIDE 70

Comparison-Based Sorting Algorithms

TWorst TBest TAvg Space Stable?

Insertion Sort

Shell Sort

Heap Sort

Merge Sort

Quick Sort

*depends on increment sequence

gray entries: not shown in class

23

slide-71
SLIDE 71

Comparison-Based Sorting Algorithms

TWorst TBest TAvg Space Stable?

Insertion Sort

Shell Sort

Heap Sort

Merge Sort

Quick Sort

*depends on increment sequence

gray entries: not shown in class

worst case lower bound on comparison based general sorting! Can we do better if we make some assumptions?

23

slide-72
SLIDE 72

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A count 1 2 3 4 5 6 7 8 9

24

slide-73
SLIDE 73

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1

count 1 2 3 4 5 6 7 8 9

25

slide-74
SLIDE 74

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1

count 1 2 3 4 5 6 7 8 9

1

26

slide-75
SLIDE 75

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1 1

count 1 2 3 4 5 6 7 8 9

1

27

slide-76
SLIDE 76

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1 1 1

count 1 2 3 4 5 6 7 8 9

1

28

slide-77
SLIDE 77

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1 2 1

count 1 2 3 4 5 6 7 8 9

1

29

slide-78
SLIDE 78

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1 1 2 1

count 1 2 3 4 5 6 7 8 9

1

30

slide-79
SLIDE 79

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1 1 1 2 1

count 1 2 3 4 5 6 7 8 9

1

31

slide-80
SLIDE 80

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

32

slide-81
SLIDE 81

Bucket Sort

  • Assume we know there are M possible values.
  • Keep an array count of length M.
  • Scan through the input array A and for each i

increment count[Ai].


2 4 6 1 1 8 2 3

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

O(N)

32

slide-82
SLIDE 82

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.
 A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

33

slide-83
SLIDE 83

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


1 1

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

34

slide-84
SLIDE 84

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

35

slide-85
SLIDE 85

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

36

slide-86
SLIDE 86

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 4 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

37

slide-87
SLIDE 87

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 4 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

38

slide-88
SLIDE 88

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 4 6 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

39

slide-89
SLIDE 89

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 4 6 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

40

slide-90
SLIDE 90

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 4 6 8 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

41

slide-91
SLIDE 91

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 4 6 8 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

42

slide-92
SLIDE 92

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 4 6 8 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

O(M)

42

slide-93
SLIDE 93

Bucket Sort

  • Then iterate through count.For each i write

count[i] copies of i to A.


3 4 6 8 1 1 2 2

A

1 1 2 2 1

count 1 2 3 4 5 6 7 8 9

1

O(M)

Total time for Bucket Sort:O(N +M)

42

slide-94
SLIDE 94

Radix Sort

  • Generalization of Bucket sort for Large M.
  • Assume M contains all base b numbers up to bp-1


(e.g. all base-10 integers up to 103)

  • Do p passes over the data, using Bucket Sort for

each digit.

  • Bucket sort is stable!

064 008 216 512 027 729 000 001 343 125

43

slide-95
SLIDE 95

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064

  • Bucket sort according

to least significant digit.

44

slide-96
SLIDE 96

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008

  • Bucket sort according

to least significant digit.

45

slide-97
SLIDE 97

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216

  • Bucket sort according

to least significant digit.

46

slide-98
SLIDE 98

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216 512

  • Bucket sort according

to least significant digit.

47

slide-99
SLIDE 99

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216 512 027

  • Bucket sort according

to least significant digit.

48

slide-100
SLIDE 100

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216 512 027 729

  • Bucket sort according

to least significant digit.

49

slide-101
SLIDE 101

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216 512 027 729 000

  • Bucket sort according

to least significant digit.

50

slide-102
SLIDE 102

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216 512 027 729 000 001

  • Bucket sort according

to least significant digit.

51

slide-103
SLIDE 103

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216 512 027 729 000 001 003

  • Bucket sort according

to least significant digit.

52

slide-104
SLIDE 104

064 008 216 512 027 729 000 001 343 125

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216 512 027 729 000 001 003 125

  • Bucket sort according

to least significant digit.

53

slide-105
SLIDE 105

1 2 3 4 5 6 7 8 9

Radix Sort

064 008 216 512 027 729 000 001 003 125 000 001 512 343 064 125 216 027 008 729

  • read off new sequence

54

slide-106
SLIDE 106

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000

  • Bucket sort according

to second-least significant digit.

55

slide-107
SLIDE 107

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001

  • Bucket sort according

to second-least significant digit.

56

slide-108
SLIDE 108

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001 512

  • Bucket sort according

to second-least significant digit.

57

slide-109
SLIDE 109

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001 512 343

  • Bucket sort according

to second-least significant digit.

58

slide-110
SLIDE 110

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001 512 343 064

  • Bucket sort according

to second-least significant digit.

59

slide-111
SLIDE 111

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125

  • Bucket sort according

to second-least significant digit.

60

slide-112
SLIDE 112

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125 216

  • Bucket sort according

to second-least significant digit.

61

slide-113
SLIDE 113

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125 216 027

  • Bucket sort according

to second-least significant digit.

62

slide-114
SLIDE 114

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125 216 027 008

  • Bucket sort according

to second-least significant digit.

63

slide-115
SLIDE 115

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 512 343 064 125 216 027 008 729 000 001 512 343 064 125 216 027 008 729

  • Bucket sort according

to second-least significant digit.

64

slide-116
SLIDE 116

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 008 512 216 125 027 729 343 064 000 001 512 343 064 125 216 027 008 729

  • read off new sequence

65

slide-117
SLIDE 117

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 008 512 216 125 027 729 343 064

  • Bucket sort according

to third-least significant digit. 000 001 008 512 216 125 027 729 343 064

66

slide-118
SLIDE 118

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 008 027 064 125 216 343 512 729 000 001 008 512 216 125 027 729 343 064

  • read off new sequence
  • Sorted!

67

slide-119
SLIDE 119

1 2 3 4 5 6 7 8 9

Radix Sort

000 001 008 027 064 125 216 343 512 729 000 001 008 512 216 125 027 729 343 064

  • read off new sequence
  • Sorted!

Each Bucket Sort: O(N+b) There are p Bucket Sorts, 
 so total time for Radix sort: O(p (N+b))

67

slide-120
SLIDE 120

a b c d e

Sorting Strings with Radix Sort

… 97 98 99 100 101 … 256 bad fad bid die pie pre dna bob nib sic

  • f

by in

68