csci 6212 design and analysis of algorithms which algorithm is better ?

21
CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ? Dr. Juman Byun The George Washington University Please drop this course if you have not taken the following prerequisite. Sometimes enthusiasm alone is not enough. CSci 1311: Discrete Structures I (3) CSci 1112: Algorithms and Data Structures (3)

Upload: alaula

Post on 06-Feb-2016

72 views

Category:

Documents


0 download

DESCRIPTION

CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?. Dr. Juman Byun The George Washington University. Please drop this course if you have not taken the following prerequisite. Sometimes enthusiasm alone is not enough. CSci 1311: Discrete Structures I (3) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

CSCI 6212 Design and Analysis of Algorithms

Which algorithm is better ?

Dr. Juman ByunThe George Washington University

Please drop this course if you have not taken the following prerequisite.

Sometimes enthusiasm alone is not enough.

• CSci 1311: Discrete Structures I (3)• CSci 1112: Algorithms and Data Structures

(3)

Page 2: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Running Time Calculation

Page 3: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Example: Running Time Analysis

Insertion Sort (A)1 for j = 2 to A.length2 key = A[j]3 // Insert A[j] into the sorted sequence A[1..j-1]4 i = j - 15 while i > 0 and A[i] > key6 A[i +1] = A[i]7 i = i - 18 A[i + 1] = key

LineLine CostCost TimeTimess

11 c1 n

22 c2 n-1

33 c3 n-1

44 c4 n-1

55 c5

66 c6

77 c7

88 c8 n-1

Page 4: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

LineLine CostCost TimeTimess

11 c1 n

22 c2 n-1

33 c3 n-1

44 c4 n-1

55 c5

66 c6

77 c7

88 c8 n-1

Page 5: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Best Case: already sorted

Insertion Sort (A)1 for j = 2 to A.length2 key = A[j]3 // Insert A[j] into the sorted sequence A[1..j-1]4 i = j - 15 while i > 0 and A[i] > key6 A[i +1] = A[i]7 i = i - 18 A[i + 1] = key

Page 6: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Best Case: already sorted

Insertion Sort (A)1 for j = 2 to A.length2 key = A[j]3 // Insert A[j] into the sorted sequence A[1..j-1]4 i = j - 15 while i > 0 and A[i] > key6 A[i +1] = A[i]7 i = i - 18 A[i + 1] = key

Page 7: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Best Case: already sorted

Page 8: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Best Case: already sorted

• simply express it

Page 9: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Insertion Sort (A)1 for j = 2 to A.length2 key = A[j]3 // Insert A[j] into the sorted sequence A[1..j-1]4 i = j - 15 while i > 0 and A[i] > key6 A[i +1] = A[i]7 i = i - 18 A[i + 1] = key

Page 10: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Insertion Sort (A)1 for j = 2 to A.length2 key = A[j]3 // Insert A[j] into the sorted sequence A[1..j-1]4 i = j - 15 while i > 0 and A[i] > key // for entire A[1..j-1]6 A[i +1] = A[i] //∴tj = (j - 1) + 17 i = i - 1 // = j8 A[i + 1] = key

Page 11: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Page 12: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Page 13: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Page 14: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Page 15: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Page 16: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Page 17: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Page 18: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

Page 19: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

• To abstract running time T(n) using constants

• Notation of "Worst-Case Running Time of Insertion Sort"

Page 20: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

Worst Case: reverse sorted

• Simply abstract it using constants

Page 21: CSCI 6212 Design and Analysis of Algorithms Which algorithm is better ?

To Denote Relative Algorithm Performance

• Algorithm 1 input size n with running time f(n)

• Asymptotic Notation