Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel - - PowerPoint PPT Presentation

Data Structures in Java Lecture 14: Sorting I 11/9/2015 Daniel Bauer 1 Sorting Midterm Exams 2 Sorting Input: 34 8 64 51 32 21 Array containing unordered Comparables (duplicates allowed). Output: 8 21 32 34 51 64


slide-1
SLIDE 1

Data Structures in Java

Lecture 14: Sorting I

11/9/2015 Daniel Bauer

1

slide-2
SLIDE 2

Sorting Midterm Exams

2

slide-3
SLIDE 3

Sorting

  • Input:
  • Array containing unordered Comparables

(duplicates allowed).

  • Output:
  • A sorted array containing the same items.
  • Only comparisons between pairs of items allowed

(comparison based sorting).

34 8 64 51 32 21 8 21 32 34 51 64

3

slide-4
SLIDE 4

Sorting Applications

4

slide-5
SLIDE 5

Sorting Applications

  • Sorting email by date / subject …, Sorting files by name.

4

slide-6
SLIDE 6

Sorting Applications

  • Sorting email by date / subject …, Sorting files by name.
  • Selection problem (find the k-th largest, find the median).

4

slide-7
SLIDE 7

Sorting Applications

  • Sorting email by date / subject …, Sorting files by name.
  • Selection problem (find the k-th largest, find the median).
  • Efficient search (binary search on sorted data).

4

slide-8
SLIDE 8

Sorting Applications

  • Sorting email by date / subject …, Sorting files by name.
  • Selection problem (find the k-th largest, find the median).
  • Efficient search (binary search on sorted data).
  • Finding duplicates.

4

slide-9
SLIDE 9

Sorting Applications

  • Sorting email by date / subject …, Sorting files by name.
  • Selection problem (find the k-th largest, find the median).
  • Efficient search (binary search on sorted data).
  • Finding duplicates.
  • Greedy algorithms (explore k highest scoring paths first).

4

slide-10
SLIDE 10

Sorting Applications

  • Sorting email by date / subject …, Sorting files by name.
  • Selection problem (find the k-th largest, find the median).
  • Efficient search (binary search on sorted data).
  • Finding duplicates.
  • Greedy algorithms (explore k highest scoring paths first).

4

slide-11
SLIDE 11

Sorting Overview

  • We will discuss different sorting algorithms and compare their

running time, required space, and stability.

  • Insertion sort
  • Shell sort
  • Heap sort
  • Merge sort
  • Quick sort
  • Radix Sort

5

slide-12
SLIDE 12

Insertion Sort

34 8 64 51 32 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position. p=1

6

slide-13
SLIDE 13

Insertion Sort

34 8 64 51 32 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position. p=1

6

slide-14
SLIDE 14

Insertion Sort

34 8 64 51 32 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8

p=2

64

6

slide-15
SLIDE 15

Insertion Sort

34 8 32 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64

p=3

51

6

slide-16
SLIDE 16

Insertion Sort

34 8 32 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64

p=3

51

6

slide-17
SLIDE 17

Insertion Sort

34 8 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32

p=4

6

slide-18
SLIDE 18

Insertion Sort

34 8 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32

p=4

6

slide-19
SLIDE 19

Insertion Sort

34 8 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32

p=4

6

slide-20
SLIDE 20

Insertion Sort

34 8 21

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32

p=4

6

slide-21
SLIDE 21

Insertion Sort

34 8

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32 21

p=5

6

slide-22
SLIDE 22

Insertion Sort

34 8

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32 21

p=5

6

slide-23
SLIDE 23

Insertion Sort

34 8

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32 21

p=5

6

slide-24
SLIDE 24

Insertion Sort

34 8

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32 21

p=5

6

slide-25
SLIDE 25

Insertion Sort

34 8

  • Perform p=1…N-1 passes through the array.
  • Assume array[0..p-1] is already sorted.
  • Take the element x at position p.
  • Repeatedly swap x its left neighbor until


it is in the correct position.

8 64 51 32 21

p=5

6

slide-26
SLIDE 26

Insertion Sort

void ¡insertionSort( ¡Integer ¡[ ¡] ¡a ¡) ¡{ ¡ ¡ ¡ ¡ ¡int ¡j; ¡ ¡ ¡ ¡ ¡for( ¡int ¡p ¡= ¡1; ¡p ¡< ¡a.length; ¡p++ ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡x ¡= ¡a[ ¡p ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡j ¡= ¡p; ¡j ¡> ¡0 ¡&& ¡x ¡< ¡a[j ¡-­‑ ¡1]; ¡j-­‑-­‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡a[ ¡j ¡-­‑ ¡1 ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡x; ¡ ¡ ¡ ¡ ¡} ¡ }

7

slide-27
SLIDE 27

Insertion Sort

void ¡insertionSort( ¡Integer ¡[ ¡] ¡a ¡) ¡{ ¡ ¡ ¡ ¡ ¡int ¡j; ¡ ¡ ¡ ¡ ¡for( ¡int ¡p ¡= ¡1; ¡p ¡< ¡a.length; ¡p++ ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡x ¡= ¡a[ ¡p ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡j ¡= ¡p; ¡j ¡> ¡0 ¡&& ¡x ¡< ¡a[j ¡-­‑ ¡1]; ¡j-­‑-­‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡a[ ¡j ¡-­‑ ¡1 ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡x; ¡ ¡ ¡ ¡ ¡} ¡ }

O(N) O(N)

Total: O(N2)

7

slide-28
SLIDE 28

Insertion Sort

void ¡insertionSort( ¡Integer ¡[ ¡] ¡a ¡) ¡{ ¡ ¡ ¡ ¡ ¡int ¡j; ¡ ¡ ¡ ¡ ¡for( ¡int ¡p ¡= ¡1; ¡p ¡< ¡a.length; ¡p++ ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡x ¡= ¡a[ ¡p ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡j ¡= ¡p; ¡j ¡> ¡0 ¡&& ¡x ¡< ¡a[j ¡-­‑ ¡1]; ¡j-­‑-­‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡a[ ¡j ¡-­‑ ¡1 ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡x; ¡ ¡ ¡ ¡ ¡} ¡ }

O(N) O(N)

Total: O(N2) Best case input (sorted): O(N)

7

slide-29
SLIDE 29

Insertion Sort

void ¡insertionSort( ¡Integer ¡[ ¡] ¡a ¡) ¡{ ¡ ¡ ¡ ¡ ¡int ¡j; ¡ ¡ ¡ ¡ ¡for( ¡int ¡p ¡= ¡1; ¡p ¡< ¡a.length; ¡p++ ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Integer ¡x ¡= ¡a[ ¡p ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡j ¡= ¡p; ¡j ¡> ¡0 ¡&& ¡x ¡< ¡a[j ¡-­‑ ¡1]; ¡j-­‑-­‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡a[ ¡j ¡-­‑ ¡1 ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡j ¡] ¡= ¡x; ¡ ¡ ¡ ¡ ¡} ¡ }

O(N) O(N)

Total: O(N2) Worst case input (sorted in reverse order): Best case input (sorted): O(N)

7

slide-30
SLIDE 30

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

34 8 64 51 32 21 7 30 1

h3 = 5

2 5

h2 = 3 h1 = 1

8

slide-31
SLIDE 31

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

34 8 64 51 32 21 7 30 1

h3 = 5

2 5

h2 = 3 h1 = 1

8

slide-32
SLIDE 32

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 8 64 51 32 34 7 30 1

h3 = 5

2 5

h2 = 3 h1 = 1

9

slide-33
SLIDE 33

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 8 64 51 32 34 7 30 1

h3 = 5

2 5

h2 = 3 h1 = 1

9

slide-34
SLIDE 34

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 64 51 32 34 8 30 1

h3 = 5

2 5

h2 = 3 h1 = 1

10

slide-35
SLIDE 35

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 64 51 32 34 8 30 1

h3 = 5

2 5

h2 = 3 h1 = 1

10

slide-36
SLIDE 36

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 30 51 32 34 8 64 1

h3 = 5

2 5

h2 = 3 h1 = 1

11

slide-37
SLIDE 37

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 30 51 32 34 8 64 1

h3 = 5

2 5

h2 = 3 h1 = 1

11

slide-38
SLIDE 38

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 32 34 8 64 51

h3 = 5

2 5

h2 = 3 h1 = 1

12

slide-39
SLIDE 39

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 32 34 8 64 51

h3 = 5

2 5

h2 = 3 h1 = 1

12

slide-40
SLIDE 40

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 2 34 8 64 51

h3 = 5

32 5

h2 = 3 h1 = 1

13

slide-41
SLIDE 41

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 2 34 8 64 51

h3 = 5

32 5

h2 = 3 h1 = 1

13

slide-42
SLIDE 42

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

21 7 30 1 2 34 8 64 51

h3 = 5

32 5

h2 = 3 h1 = 1

13

slide-43
SLIDE 43

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

5 7 30 1 2 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

14

slide-44
SLIDE 44

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

5 7 30 1 2 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

14

slide-45
SLIDE 45

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 7 30 5 2 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

15

slide-46
SLIDE 46

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 7 30 5 2 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

15

slide-47
SLIDE 47

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 30 5 7 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

16

slide-48
SLIDE 48

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 30 5 7 21 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

16

slide-49
SLIDE 49

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

17

slide-50
SLIDE 50

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

18

slide-51
SLIDE 51

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

19

slide-52
SLIDE 52

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

20

slide-53
SLIDE 53

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

21

slide-54
SLIDE 54

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 64 51

h3 = 5

32 34

h2 = 3 h1 = 1

21

slide-55
SLIDE 55

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

22

slide-56
SLIDE 56

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

23

slide-57
SLIDE 57

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

24

slide-58
SLIDE 58

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 21 5 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

24

slide-59
SLIDE 59

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 21 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

25

slide-60
SLIDE 60

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 21 7 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

25

slide-61
SLIDE 61

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 21 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

26

slide-62
SLIDE 62

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 21 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

27

slide-63
SLIDE 63

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 21 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

27

slide-64
SLIDE 64

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 21 30 8 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

27

slide-65
SLIDE 65

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

28

slide-66
SLIDE 66

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

29

slide-67
SLIDE 67

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

30

slide-68
SLIDE 68

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

30

slide-69
SLIDE 69

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 34 51

h3 = 5

32 64

h2 = 3 h1 = 1

30

slide-70
SLIDE 70

Shell Sort

  • Generalize insertion sort so that items that are

further apart can be swapped.

  • Break up sorting into phases. Each phase k makes

sure that all items space hk apart are sorted.

  • “increment sequence” of steps h1, h2, … ,ht

1 2 5 7 8 21 30 32 34

h3 = 5

51 64

h2 = 3 h1 = 1

31

slide-71
SLIDE 71

Shell Sort

  • The running time analysis for shell sort is complex

and depends on the specific increment sequence.

  • With Hibbard’s sequence (1,3,7,15,…,2k-1)


worst case running time is

32

slide-72
SLIDE 72

Sorting Stability

  • Assume we put key/value pairs sorted by keys into the


array.

  • Shell Sort is unstable: keys will be sorted, but values 


for the same key may be in different order than in the 
 input.

34 8 64 1 4 3 30 7 2 5

val1

1

val233

slide-73
SLIDE 73

Sorting Stability

  • Assume we put key/value pairs sorted by keys into the


array.

  • Shell Sort is unstable: keys will be sorted, but values 


for the same key may be in different order than in the 
 input.

34 8 64 1 4 3 30 7 2 5

val1

1

val2

33

slide-74
SLIDE 74

Space Requirements

  • Both Insertion Sort and Shell Sort operate in place.
  • Only a small amount of memory required to store a 


temporary value for swaps.

  • Space requirement: O(1)

34

slide-75
SLIDE 75

Heap Sort

  • First convert an unordered array into a heap in

O(N) time.

  • Then perform N deleteMin operations to retrieve

the elements in sorted order.

  • each deleteMin is O(log N)

35

slide-76
SLIDE 76

Heap Sort

  • First convert an unordered array into a heap in

O(N) time.

  • Then perform N deleteMin operations to retrieve

the elements in sorted order.

  • each deleteMin is O(log N)
  • Problem: This algorithm requires a second array to


store the output: O(N) space!


  • Idea: re-use the freed space after each deleteMin.

35

slide-77
SLIDE 77

Heap Sort Example

5 4 6 9 1 8 3 10 7 2 11

36

slide-78
SLIDE 78

Heap Sort Example

1 2 3 4 5 6 7 8 9

10 11

5 4 6 9 1 8 3 10 7 2 11

36

slide-79
SLIDE 79

Heap Sort Example

6 3 7 8 9

10

2 1 4 5

11

1 2 3 7 4 8 6 10 9 5 11

Build heap in O(N) time

37

slide-80
SLIDE 80

Heap Sort Example

6 3 7 8 9

10

2 1 4 5

11

1 2 3 7 4 8 6 10 9 5 11

Build heap in O(N) time

37

slide-81
SLIDE 81

Heap Sort Example

6 3 7 8 9

10

2 1 4 5

11

2 3 7 4 8 6 10 9 5

1 11 deleteMin, write min element into empty cell

38

slide-82
SLIDE 82

Heap Sort Example

6 3 7 8 9

10

2 4 5

11

2 3 7 4 8 6 10 9 5

1 11 deleteMin, write min element into empty cell

38

slide-83
SLIDE 83

Heap Sort Example

6 3 7 8 9

10

4 2 5 11

2 4 3 7 5 8 6 10 9 11

1 Percolate down

39

slide-84
SLIDE 84

Heap Sort Example

6 3 7 8 9

10

4 2 5 11

4 3 7 5 8 6 10 9

1 11 2 deleteMin, write min element into empty cell

40

slide-85
SLIDE 85

Heap Sort Example

6 3 7 8 9

10

4 5 11

4 3 7 5 8 6 10 9

1 11 2 deleteMin, write min element into empty cell

40

slide-86
SLIDE 86

Heap Sort Example

11 6 7 8 9

10

4 3 5

4 6 7 5 8 11 10 9

1 2 3 Percolate down

41

slide-87
SLIDE 87

Heap Sort Example

11 6 7 8 9

10

4 5

5 6 7 9 8 11 10 3

1 2 4 deleteMin, write min element into empty cell

42

slide-88
SLIDE 88

Heap Sort Example

11 6 10 8 9 5 7

7 6 10 9 8 11 4 3

1 2 5 deleteMin, write min element into empty cell

43

slide-89
SLIDE 89

Heap Sort Example

8 10 11 9 6 7

7 8 10 9 11 5 4 3

1 2 6 deleteMin, write min element into empty cell

44

slide-90
SLIDE 90

Heap Sort Example

8 10 11 7 9

9 8 10 11 6 5 4 3

1 2 7 deleteMin, write min element into empty cell

45

slide-91
SLIDE 91

Heap Sort Example

11 10 8 9

9 11 10 7 6 5 4 3

1 2 8 deleteMin, write min element into empty cell

46

slide-92
SLIDE 92

Heap Sort Example

11 9 10

10 11 8 7 6 5 4 3

1 2 9 deleteMin, write min element into empty cell

47

slide-93
SLIDE 93

Heap Sort Example

10 11

11 9 8 7 6 5 4 3

1 2 10 deleteMin, write min element into empty cell

48

slide-94
SLIDE 94

Heap Sort Example

11

10 9 8 7 6 5 4 3

1 2 11

  • Can use a max-heap if we want the output in 


increasing order.

49

slide-95
SLIDE 95

Merge Sort

  • A classic divide-and-conquer algorithm.
  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

51 32 21 1 34 8 64 2

50

slide-96
SLIDE 96

Merge Sort

  • A classic divide-and-conquer algorithm.
  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

51 32 21 1 34 8 64 2

50

slide-97
SLIDE 97

Merge Sort

1 21 32 51 2 8 34 64

  • A classic divide-and-conquer algorithm.
  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

51

slide-98
SLIDE 98

Merge Sort

1 21 32 51 2 8 34 64

1 2 8 21 32 34 51 64

  • A classic divide-and-conquer algorithm.
  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

51

slide-99
SLIDE 99

Merging Sorted Sublists

1 21 32 51 2 8 34 64

Actr Bctr Cctr

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-100
SLIDE 100

Merging Sorted Sublists

1 21 32 51 2 8 34 64

Actr Bctr Cctr

1

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-101
SLIDE 101

Merging Sorted Sublists

1 21 32 51 2 8 34 64

Actr Bctr Cctr

1 2

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-102
SLIDE 102

Merging Sorted Sublists

1 21 32 51 2 8 34 64

Actr Bctr Cctr

1 2 8

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-103
SLIDE 103

Merging Sorted Sublists

1 21 32 51 2 8 34 64

Actr Bctr Cctr

1 2 8 21

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-104
SLIDE 104

Merging Sorted Sublists

1 21 32 51 2 8 34 64

Actr Bctr Cctr

1 2 8 21 32

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-105
SLIDE 105

Merging Sorted Sublists

1 21 32 51 2 8 34 64

Actr Bctr Cctr

1 2 8 21 32 34

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-106
SLIDE 106

Merging Sorted Sublists

1 21 32 51 2 8 34 64

Actr Cctr

1 2 8 21 32 34 51

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-107
SLIDE 107

Merging Sorted Sublists

1 21 32 51 2 8 34 64

1 2 8 21 32 34 51 64

  • Keep a pointers for each sub-list in the array.
  • In each step, compare the elements they point two.
  • If a[Actr] < a[Bctr], copy a[Actr] to tmp and advance Actr.
  • Otherwise, copy a[Bctr] to the output and advance Bctr.

tmp a

52

slide-108
SLIDE 108

Merging Sorted Sublists

¡ ¡ ¡ ¡private ¡static ¡<T ¡extends ¡Comparable<T>> ¡ ¡ ¡ ¡ ¡void ¡merge( ¡T[] ¡a, ¡T[] ¡tmpArray, ¡int ¡aCtr, ¡int ¡bCtr, ¡int ¡rightEnd ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡leftEnd ¡= ¡bCtr ¡-­‑ ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡tmpPos ¡= ¡aCtr; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡numElements ¡= ¡rightEnd ¡-­‑ ¡aCtr ¡+ ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Main ¡loop ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while( ¡aCtr ¡<= ¡leftEnd ¡&& ¡bCtr ¡<= ¡rightEnd ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if( ¡a[ ¡aCtr ¡].compareTo( ¡a[ ¡bCtr ¡] ¡) ¡<= ¡0 ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmpArray[ ¡tmpPos++ ¡] ¡= ¡a[ ¡aCtr++ ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmpArray[ ¡tmpPos++ ¡] ¡= ¡a[ ¡bCtr++ ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while( ¡aCtr ¡<= ¡leftEnd ¡) ¡ ¡ ¡ ¡// ¡Copy ¡rest ¡of ¡first ¡half ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmpArray[ ¡tmpPos++ ¡] ¡= ¡a[ ¡aCtr++ ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡while( ¡bCtr ¡<= ¡rightEnd ¡) ¡ ¡// ¡Copy ¡rest ¡of ¡right ¡half ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡tmpArray[ ¡tmpPos++ ¡] ¡= ¡a[ ¡bCtr++ ¡]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Copy ¡tmpArray ¡back ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡for( ¡int ¡i ¡= ¡0; ¡i ¡< ¡numElements; ¡i++, ¡rightEnd-­‑-­‑ ¡) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡a[ ¡rightEnd ¡] ¡= ¡tmpArray[ ¡rightEnd ¡]; ¡ ¡ ¡ ¡ ¡}

53

slide-109
SLIDE 109

Merge Sort

  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

51 32 21 1 34 8 64 2

54

slide-110
SLIDE 110

Merge Sort

  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

51 32 21 1 34 8 64 2

55

slide-111
SLIDE 111

Merge Sort

  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

51 32 21 1 34 8 64 2

56

slide-112
SLIDE 112

Merge Sort

  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

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

57

slide-113
SLIDE 113

Merge Sort

  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

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

58

slide-114
SLIDE 114

Merge Sort

  • Split the array in half, recursively sort each half.
  • Merge the two sorted lists.

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

59

slide-115
SLIDE 115

Merge Sort - Implementation

private ¡static ¡<T ¡extends ¡Comparable<T>> ¡ void ¡mergeSort( ¡T[] ¡a, ¡T[] ¡tmpArray, ¡int ¡left, ¡int ¡right ¡) ¡ ¡ ¡ ¡ ¡ ¡if( ¡left ¡< ¡right ¡) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡center ¡= ¡( ¡left ¡+ ¡right ¡) ¡/ ¡2; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡mergeSort( ¡a, ¡tmpArray, ¡left, ¡center ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡mergeSort( ¡a, ¡tmpArray, ¡center ¡+ ¡1, ¡right ¡); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡merge( ¡a, ¡tmpArray, ¡left, ¡center ¡+ ¡1, ¡right ¡); ¡ ¡ ¡ ¡ ¡} ¡ }

60

slide-116
SLIDE 116

Merge Sort Running Time

  • This running time analysis is typical for divide and

conquer algorithms.

  • Merge sort is a recursive algorithm. The running

time analysis should be similar to what we have seen for other algorithms of this type (e.g. binary search)

  • Base case: N=1 (sort a 1-element list). T(1) = 1
  • Recurrence: T(N) = 2 T(N/2) + N

Recursively sort each half Merge the two halfs

61

slide-117
SLIDE 117

Merge Sort Running Time

assume

62

slide-118
SLIDE 118

Merge Sort Properties

  • Worst case running time:
  • Is MergeSort stable?


  • Space requirement? 


63

slide-119
SLIDE 119

Merge Sort Properties

  • Worst case running time:
  • Is MergeSort stable?


  • Space requirement? 


  • Yes. Merging preservers order of elements.

63

slide-120
SLIDE 120

Merge Sort Properties

  • Worst case running time:
  • Is MergeSort stable?


  • Space requirement? 


  • Yes. Merging preservers order of elements.

Need a temporary array. O(N)

63