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

Post on 06-Feb-2016

64 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

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)

Running Time Calculation

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

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

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

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

Best Case: already sorted

Best Case: already sorted

• simply express it

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

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

Worst Case: reverse sorted

Worst Case: reverse sorted

Worst Case: reverse sorted

Worst Case: reverse sorted

Worst Case: reverse sorted

Worst Case: reverse sorted

Worst Case: reverse sorted

Worst Case: reverse sorted

Worst Case: reverse sorted

• To abstract running time T(n) using constants

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

Worst Case: reverse sorted

• Simply abstract it using constants

To Denote Relative Algorithm Performance

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

• Asymptotic Notation

top related