merge sort data structures and algorithms cs 244 brent m. dingle, ph.d. department of mathematics,...

105
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content from Data Structures Using C++ (D.S. Malik)

Upload: ella-reynolds

Post on 18-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort

Data Structures and Algorithms

CS 244

Brent M. Dingle, Ph.D.

Department of Mathematics, Statistics, and Computer Science

University of Wisconsin – Stout

Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount)Some content from Data Structures Using C++ (D.S. Malik)

Page 2: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Previously

• Sorts Already Seen• Insertion Sort• Selection Sort• Bubble Sort

• Related• Linear (sequential) Search• Binary Search

Page 3: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort• We are jumping ahead in the book

• Chap 11.1• Mostly so we don’t run out of time and miss this• But also because it fits in several different ways

• Goes with sorting• Goes with Big Oh• Shows how to think using binary trees

• Example of application before theory

• And then we will jump back to Chap 6

Page 4: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort (the Picture)7 2 9 4 2 4 7 9

7 2 2 7 9 4 4 9

7 7 2 2 9 9 4 4

Page 5: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Algorithm

• Merge sort is based on the divide-and-conquer paradigm. It consists of three steps:– Divide: partition input

sequence S into two sequences S1 and S2 of about n/2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S, comparator C Output sequence S sorted

according to Cif S.size() > 1 {

(S1, S2) := partition(S, S.size()/2)

S1 := mergeSort(S1, C)S2 := mergeSort(S2, C)S := merge(S1, S2)

} return(S)

Page 6: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Start at the top node --- or rather the ROOT

• computer science trees grow upside down

7 2 9 4 3 8 6 1

Page 7: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Partition the numbers into 2 sets (left half and right half)

7 2 9 4 3 8 6 1

Page 8: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Create child nodes

7 2 9 4 3 8 6 1

7 2 9 4 3 8 6 1

Page 9: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on ‘left’ child

• Results in partitioning it into 2 sets (left and right)

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

Page 10: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on ‘left’ child

• Results in partitioning it into 2 sets (left and right)

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

Page 11: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Create 2 child nodes

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

Page 12: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on ‘left’ child

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

Page 13: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Partition into 2 sets

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

Page 14: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Partition into 2 sets, left and right

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

Page 15: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Create 2 child nodes

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

7 2

Page 16: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call Merge Sort on ‘left’ child

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

27

Page 17: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Single element is already ordered by definition, results in 7

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

27 7

Page 18: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call Merge Sort on ‘right’ child

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

7 7 2

Page 19: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Single element is already ordered by definition, results in 2

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

7 7 2 2

Page 20: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call Merge on the two sequences: { 7 } and { 2 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 9 4

7 7 2 2

Page 21: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call Merge on the two sequences: { 7 } and { 2 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 9 4

7 7 2 2

Page 22: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call Merge on the two sequences: { 7 } and { 2 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 7 9 4

7 7 2 2

Page 23: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Results in ordered sequence of { 2 7 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 7 9 4

7 7 2 2

Page 24: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on ‘right’ child

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 7

7 7 2 2

9 4

Page 25: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Partition and create child nodes

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 7

7 7 2 2

9 4

9 4

Page 26: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on left child

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 7

7 7 2 2

9 4

9 9 4

Page 27: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on right child

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 7

7 7 2 2

9 4

9 9 4 4

Page 28: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge Sequences: { 9 } and { 4 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

Page 29: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge Sequences: { 2 7 } and { 4 9 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

Page 30: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge Sequences: { 2 7 } and { 4 9 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4 2

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

Page 31: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge Sequences: { 2 7 } and { 4 9 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4 2 4

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

Page 32: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge Sequences: { 2 7 } and { 4 9 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

Page 33: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge Sequences: { 2 7 } and { 4 9 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

Page 34: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Result is{ 2 4 7 9 }

3 8 6 1

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

Page 35: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on right side

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

Page 36: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Partition and create children

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

3 8 6 1

Page 37: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on left child

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

6 13 8

Page 38: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Partition and create child nodes

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

6 13 8

3 8

Page 39: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on left child

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

6 13 8

83 3

Page 40: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on right child

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

6 13 8

3 3 8 8

Page 41: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge Results

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

6 13 8 3 8

3 3 8 8

Page 42: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on right child

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

3 8 3 8

3 3 8 8

6 1

Page 43: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Partition and create children

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

3 8 3 8

3 3 8 8

6 1

6 1

Page 44: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on left child

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

3 8 3 8

3 3 8 8

6 1

16 6

Page 45: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Call MergeSort on right child

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

3 8 3 8

3 3 8 8

6 1

6 6 1 1

Page 46: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge results { 6 } and { 1 }

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 6 1

3 8 3 8

3 3 8 8 6 6 1 1

6 1 1 6

Page 47: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge results { 3 8 } and { 1 6 }

7 2 9 4 3 8 6 1

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 3 8

3 3 8 8 6 6 1 1

6 1 1 6

3 8 6 1 1 3 6 8

Page 48: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge results { 2 4 7 9 } and { 1 3 6 8 }

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 3 8

3 3 8 8 6 6 1 1

6 1 1 6

3 8 6 1 1 3 6 8

7 2 9 4 3 8 6 1

Page 49: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort Execution Tree

• An execution of merge-sort is depicted by a binary tree– Merge results { 2 4 7 9 } and { 1 3 6 8 }

7 2 9 4 2 4 7 9

7 2 2 7

7 7 2 2

9 4 4 9

9 9 4 4

3 8 3 8

3 3 8 8 6 6 1 1

6 1 1 6

3 8 6 1 1 3 6 8

7 2 9 4 3 8 6 1 1 2 3 4 6 7 8 9

Page 50: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

MergeSort: Another Tree Layout• You may also see MergeSort Trees that

• instead of reversing direction back up the tree• continue down merging the branches as they go

Page 51: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

End Example Walk Through• Next

• In Class Activity

Page 52: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

In Class Activity• Complete the following MergeSort tree

C R P G B H Q A

Page 53: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

In Class Activity• First Step: Divide into two parts. Stay with Left

C R P G B H Q AC R P G B H Q A

Page 54: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

In Class Activity• Second Step: Divide again. Stay with left

C R P G B H Q A

C R P G

B H Q A

C R

Page 55: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

In Class Activity• Third Step: Divide again. Stay with Left half

C R P G B H Q AB H Q A

C RC

C R P G

Page 56: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

In Class Activity• Fourth Step: Go with Right half now

C R P G B H Q AB H Q A

C RR

C C

C R P G

Page 57: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

In Class Activity• Fifth Step: Merge { C } and { R }

C R P G B H Q AB H Q A

RC C R

C R P G

C R

C R

Page 58: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

In Class Activity• Keep Going

C R P G B H Q AB H Q A

RC C R

Pause HereFor StudentsTo Complete

C R P G

C R C R

Page 59: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Class Exercise• Complete the following MergeSort tree

C R P G C G P R B H Q A A B H Q

C R C R P G G P B H B H Q A A Q

C C R R P P G G B B H H Q Q A A

C R P G B H Q A

Enhanced Version

Page 60: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort

• Merge sort is based on the divide-and-conquer paradigm. It consists of three steps:– Divide: partition input

sequence S into two sequences S1 and S2 of about n/2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S, comparator C Output sequence S sorted

according to Cif S.size() > 1 {

(S1, S2) := partition(S, S.size()/2)

S1 := mergeSort(S1, C)S2 := mergeSort(S2, C)S := merge(S1, S2)

} return(S)

Page 61: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Runtime of Merge Sort ?

• Merge sort is based on the divide-and-conquer paradigm. It consists of three steps:– Divide: partition input

sequence S into two sequences S1 and S2 of about n/2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S, comparator C Output sequence S sorted

according to Cif S.size() > 1 {

(S1, S2) := partition(S, S.size()/2)

S1 := mergeSort(S1, C)S2 := mergeSort(S2, C)S := merge(S1, S2)

} return(S)

O( ? )

O( ? )

O( ? )

Page 62: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Runtime of Merge Sort ?

• Merge sort is based on the divide-and-conquer paradigm. It consists of three steps:– Divide: partition input

sequence S into two sequences S1 and S2 of about n/2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S, comparator C Output sequence S sorted

according to Cif S.size() > 1 {

(S1, S2) := partition(S, S.size()/2)

S1 := mergeSort(S1, C)S2 := mergeSort(S2, C)S := merge(S1, S2)

} return(S)

O( 1 )

O( ? )

O( ? )

Divide Step is O(1) if using arrays and NOT making temporary copiesmiddleIndex = minIndex + (maxIndex - minIndex)/2

Aside:If using a list or making temporary copiesusing brute force approach results in O(n) Because we have to copy or walk through n elements

Page 63: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Runtime of Merge Sort ?

• Merge sort is based on the divide-and-conquer paradigm. It consists of three steps:– Divide: partition input

sequence S into two sequences S1 and S2 of about n/2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S, comparator C Output sequence S sorted

according to Cif S.size() > 1 {

(S1, S2) := partition(S, S.size()/2)

S1 := mergeSort(S1, C)S2 := mergeSort(S2, C)S := merge(S1, S2)

} return(S)

O( 1 )

O( TBD )

O( ? )

Recursive Step:Assume initially there are N elements.Then denote the time for mergeSort to be T(N)

Because we recursively call mergeSort

T(N) will have to be >= T(N/2) + T(N/2)

Page 64: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Runtime of Merge Sort ?

• Merge sort is based on the divide-and-conquer paradigm. It consists of three steps:– Divide: partition input

sequence S into two sequences S1 and S2 of about n/2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S, comparator C Output sequence S sorted

according to Cif S.size() > 1 {

(S1, S2) := partition(S, S.size()/2)

S1 := mergeSort(S1, C)S2 := mergeSort(S2, C)S := merge(S1, S2)

} return(S)

O( 1 )

O( TBD )

O( ? )

Conquer Step:

For this we need to understand the support function merge

Page 65: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

Page 66: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

Page 67: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

1 3 6 82 4 5 9

Example

Run

This a A This a B

Page 68: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

1 3 6 82 4 5 9

Example

Run

This a S

Page 69: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

1 3 6 82 4 5 9

Example

Run

Page 70: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

3 6 82 4 5 9

1Example

Run

Page 71: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

3 6 84 5 9

1 2Example

Run

Page 72: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

6 84 5 9

1 2 3Example

Run

Page 73: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

6 85 9

1 2 3 4Example

Run

Page 74: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

6 89

1 2 3 4 5Example

Run

Page 75: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

89

1 2 3 4 5 6Example

Run

Page 76: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

9

1 2 3 4 5 6 8Example

Run

Page 77: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

1 2 3 4 5 6 8 9Example

Run

Page 78: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

That concludes the example run of a merge

But what is the run time of the algorithm (to the right) ?

Page 79: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

n/2

Page 80: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

n/2

+ n/2

Page 81: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

n/2

+ n/2

+ n/2

Page 82: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

n/2

+ n/2

+ n/2 + n/2

Page 83: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

n/2

+ n/2

+ n/2 + n/2+ n/2

Page 84: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

n/2

+ n/2

+ n/2 + n/2+ n/2 +n/2

Page 85: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

n/2

+ n/2

+ n/2 + n/2+ n/2 +n/2

+ 1

Page 86: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

n/2

+ n/2

+ n/2 + n/2+ n/2 +n/2

+ 1

3 n + 1

Page 87: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

3 n + 1

Page 88: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Merge Part (conquer)

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

3 n + 1

Page 89: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Conclusion O(n)Support Function Merge

• The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B

• Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time– M(n) = O(n)

Algorithm merge(A, B)Input sequences A and B with

n/2 elements each Output sorted sequence of A B

S empty sequencewhile A.isEmpty() B.isEmpty()

if A.first() < B.first()

S.insertLast(A.removeFirst())else

S.insertLast(B.removeFirst())while A.isEmpty() S.insertLast(A.removeFirst())while B.isEmpty() S.insertLast(B.removeFirst())return S

Notice:means NOT

Notice: means AND

Repeat

Page 90: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort

• Merge sort is based on the divide-and-conquer paradigm. It consists of three steps:– Divide: partition input

sequence S into two sequences S1 and S2 of about n/2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S, comparator C Output sequence S sorted

according to Cif S.size() > 1 {

(S1, S2) := partition(S, S.size()/2)

S1 := mergeSort(S1, C)S2 := mergeSort(S2, C)S := merge(S1, S2)

} return(S)

O( 1 )

O( TBD )

O( n )Per previous slides, we just calculated this

Page 91: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Merge Sort

• Merge sort is based on the divide-and-conquer paradigm. It consists of three steps:– Divide: partition input

sequence S into two sequences S1 and S2 of about n/2 elements each

– Recur: recursively sort S1 and S2

– Conquer: merge S1 and S2

into a unique sorted sequence

Algorithm mergeSort(S, C)Input sequence S, comparator C Output sequence S sorted

according to Cif S.size() > 1 {

(S1, S2) := partition(S, S.size()/2)

S1 := mergeSort(S1, C)S2 := mergeSort(S2, C)S := merge(S1, S2)

} return(S)

O( 1 )

O( TBD )

O( n )

Still must work out this recursive call stuff

Page 92: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

What we have so far

• By definition • T(N) = # of comparisons to mergesort an input size of N

• So• T(N) = partition time + time left half + time right half + time to merge

= constant + T( N/2 ) + T( N/2 ) + N

• Thus Mergesort recurrence: T(N) = 2*T( N/2 ) + N• for N > 1, and T(1) = 0 takes zero time to sort 1 thing

• also let’s assume N is a power of 2

• Claim T(N) = N lg N• Can we “prove” it ?

Recall: lg N denotes log2 N

Divide Recur Conquer

Page 93: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Tree Proof

• The height h of the merge-sort tree is O(log2 n)

– at each recursive call we divide the sequence in half,

• The work done at each level is O(n) – At level i, we partition and merge 2i sequences of size n / 2i

• Thus, the total running time of merge-sort is O(n log2 n)

Page 94: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Tree Proof

• The height h of the merge-sort tree is O(log2 n)

– at each recursive call we divide the sequence in half,

• The work done at each level is O(n) – At level i, we partition and merge 2i sequences of size n / 2i

• Thus, the total running time of merge-sort is O(n log2 n)

lg n

Page 95: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

= n

The Tree Proof

• The height h of the merge-sort tree is O(lg n) – at each recursive call we divide the sequence in half,

• The work done at each level is O(n) – At level i, we partition and merge 2i sequences of size n / 2i

• Thus, the total running time of merge-sort is O(n lg n)

nT(n)

T(n/2) T(n/2) + 2(n/2)lg n

= n

Page 96: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

= n

The Tree Proof

• The height h of the merge-sort tree is O(lg n) – at each recursive call we divide the sequence in half,

• The work done at each level is O(n) – At level i, we partition and merge 2i sequences of size n / 2i

• Thus, the total running time of merge-sort is O(n lg n)

nT(n)

T(n/2) T(n/2)

T(n/4) T(n/4) T(n/4) T(n/4)

+ 2(n/2)lg n

:

= n

= n+ 4(n/4)

Page 97: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

= n

= n

The Tree Proof

• The height h of the merge-sort tree is O(lg n) – at each recursive call we divide the sequence in half,

• The work done at each level is O(n) – At level i, we partition and merge 2i sequences of size n / 2i

• Thus, the total running time of merge-sort is O(n lg n)

nT(n)

T(n/2) T(n/2)

T(n/4) T(n/4) T(n/4) T(n/4)

+ 2(n/2)lg n

T(n / 2k )

:

+ 2k(n/2k)::

= n

= n+ 4(n/4)

Page 98: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

= n

= n

= n

The Tree Proof

• The height h of the merge-sort tree is O(lg n) – at each recursive call we divide the sequence in half,

• The work done at each level is O(n) – At level i, we partition and merge 2i sequences of size n / 2i

• Thus, the total running time of merge-sort is O(n lg n)

nT(n)

T(n/2) T(n/2)

T(n/4) T(n/4) T(n/4) T(n/4)

+ 2(n/2)lg n

T(n / 2k )

:

+ 2k(n/2k)

T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2)

::

+ (n/2)*2

= n

= n+ 4(n/4)

Page 99: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The Tree Proof

• The height h of the merge-sort tree is O(lg n)

• The work done at each level is O(n)

• Thus, the total running time of merge-sort is O(n lg n)

T(n)

T(n/2) T(n/2)

T(n/4) T(n/4) T(n/4) T(n/4)

lg n

T(n / 2k )

T(2) T(2) T(2) T(2) T(2) T(2) T(2) T(2)

n things lg n times

gives

= n lg n

= n

= n

= n

n

+ 2(n/2)

:

+ 2k(n/2k)::

+ (n/2)*2

= n

= n+ 4(n/4)

Page 100: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Telescoping Proof

• T(N) = 2 * T(N/2) + N Begin by dividing both sides by N• T(N) / N = 2 * T(N/2) / N + 1• = T(N/2) / (N/2) + 1 by algebra (*2 same as divide by

(1/2) )• = T(N/4) / (N/4) + 1 + 1 telescope (apply to first term)• = T(N/8) / (N/8) + 1 + 1 + 1 telescope (apply to first term)• :• = T(N/N) / (N/N) + 1 + 1 + … + 1 stop telescoping T(N/N) = T(1)

= 0• = lg N • that is• T(N) / N = lg N

• so• T(N) = N lg N

Given T(N) = 2T(N/2) + NProve T(N) = N lg N for N>1, and also given T(1) = 0

Recall: lg N denotes log2 N

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

telescope (apply to first term)

2 * ( T(N/2) / N ) = ( T(N/2) / N ) / ( 1/ 2) = T(N/2) / ( N * (1/2) ) = T(N/2) / ( N /2)

by algebra (*2 same as divide by (1/2) )

lg N integer-wise is the number of times N can be divided by 2And we have a “+1” for each time we divided by NSo we have =lg N

Page 101: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Summary of Sorting Algorithms (so far)

Algorithm Time Notes

Selection Sort O(n2) Slow, in-placeFor small data sets

Insertion Sort O(n2) Slow, in-placeFor small data sets

Bubble Sort O(n2) Slow, in-placeFor small data sets

Heap Sort O(nlog n) Fast, in-placeFor large data sets

Merge Sort O(n lg n) Fast, sequential data accessFor huge data sets

Page 102: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Graded In-Class Activity: MergeSort

• Using std::vector• Implement MergeSort on an vector of type int

• See algorithms on previous slides for basic idea• Do NOT use std::list use std::vector

• Starter Code may be available on D2L• ICA212_MergeSort.cpp

• On an INDIVIDUAL basis turn in resulting code for possible bonus points on an assignment• May work as group

• But each individual must turn in• Identify any one else you worked with in the code comments

Page 103: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Cowtoth Challenge On Which TO THink

• You are given a Singly Linked List of integers• e.g. nodeType<int> *

• You do NOT know how many items are in the list

• Write a function that has• input: a pointer to the first node in the list, pList• output: a pointer to the middle node of the list• restriction: the function may use only 3 variables

• nodeType<int> *pList (the input parameter – not to be changed)• nodeType<int> *middle (the return value)• nodeType<int> *current (a pointer for temporary use)

• Assume each node in the list has a next pointer• i.e. Given nodeType<int> *pointer = pList

pList->next will give a pointer to the next node or NULL if there is no next node

Page 104: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

Moving on to Trees

• Tree Definitions and ADT (§7.1)

• Tree Traversal Algorithms for General Trees (preorder and postorder) (§7.2)

• BinaryTrees (§7.3)• Data structures for trees (§7.1.4 and §7.3.4)• Traversals of Binary Trees (preorder, inorder,

postorder) (§7.3.6)• Euler Tours (§7.3.7)

Page 105: Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin

The End of This Part• End