Docsity

Analysis of Algorithms - Advanced Programming - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Analysis of Algorithms - Advanced Programming - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Analysis of Algorithms

Introduction using Insertion Sort

Characteristics of Algorithms

• Algorithms are precise. Each step has a clearly

defined meaning; “Deterministic”

• Algorithms are effective. The task is always

done as required; “Correct”

• Algorithms have a finite number of steps;

Algorithms must terminate. How do you

know?

Example: sorting numbered cards

1 2 3 4 5 6

Example: sorting numbered cards

1 2 3 4 5 6

1 2 3 4 5 6

Example: sorting numbered cards

1 2 3 4 5 6

1 2 3 4 5 6

Example: sorting numbered cards

1 2 3 4 5 6

1 2 3 4 5 6

Example: sorting numbered cards

1 2 3 4 5 6

1 2 3 4 5 6

Expressing computer algorithms

• It should be expressed in a language more

precise, less ambiguous, and more compact

than a “natural language” such as English;

• Algorithms are usually written in a

pseudocode and later translated to a real

programming language.

• Sometimes algorithms are “flowcharted”

using HIPO (Hierarchical Input, Processing,

and Output) symbols.

Choosing an Analysis Metric

• Estimate the running time of algorithms;

= F(Problem Size)

= F(Input Size)

= number of primitive operations used (add,

multiply, compare etc)

Analysis for Insertion Sort

Insertion-Sort(A) Cost Times ( Iterations )

1 B[1] = A[1] c 1

2 for j = 2 to n { c 2

3 i = j - 1 c 3

4 while 0 < i and A[j] < B[i] c 4

5 i = i - 1 c 5

6 for k = j downto i + 2 c 6

7 B[k] = B[k-1] c 7

8 B[i+1] = A[j] } c 8

n - 1

n - 1

n

 ^   

n j j t 2 1

 ^   

n j j t 2 1

n j j t 2

  ^ ^  ^ 

   

n

j

n

j

n

j

j j j

n

j

T n c c n c n c tj c t c t c t c n

2 2 2

5 6 7 8 2

n j j t 2

Insertion Sort Analysis (cont.)

• Worst Case:

Array in reverse order,

Note that

n n c

n n T n c c n c n c

c n

n n c

n n c

 c c c c  n  c 2 c 3 c 4 c 5 c 6 c 7 c 8  n

2  4 / 2  5 / 2  6 / 2  7 / 2    / 2  / 2  / 2  / 2 

cccccanbnc

2 1 3 4 6 8

We are usually interested in the

worst-case running time

t j

j

 for all j.

(quadratic in n)