Docsity

Design and Analysis of Computer Algorithm Chapter 4, Lecture notes of Advanced Algorithms

Partial preview of the text

Download Design and Analysis of Computer Algorithm Chapter 4 and more Lecture notes Advanced Algorithms in PDF only on Docsity!

Design and Analysis of Computer Algorithm November 26, 2019 1

Design and Analysis of

Computer Algorithm

Chapter 4

Dr. M. Mezher

Department of Computer Science

Design and Analysis of Computer Algorithm November 26, 2019 2

Divide and Conquer

Merge Sort

Insertion Sort

Mergesort: Divide Step

Mergesort: Divide Step

Mergesort: Combine Step

Mergesort: Algorithm

Mergesort: Running Time

Design and Analysis of Computer Algorithm November 26, 2019 11

SORTING

An Example: Insertion Sort

InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }

i = ๏ƒ† j = ๏ƒ† key = ๏ƒ†
A[j] = ๏ƒ† A[j+1] = ๏ƒ†

An Example: Insertion Sort

InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }

i = 2 j = 1 key = 10
A[j] = 30 A[j+1] = 10

An Example: Insertion Sort

InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }

i = 2 j = 1 key = 10
A[j] = 30 A[j+1] = 30

An Example: Insertion Sort

InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }

i = 2 j = 0 key = 10
A[j] = ๏ƒ† A[j+1] = 30

An Example: Insertion Sort

InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }

i = 2 j = 0 key = 10
A[j] = ๏ƒ† A[j+1] = 10

An Example: Insertion Sort

InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } }

i = 3 j = 0 key = 10
A[j] = ๏ƒ† A[j+1] = 10