Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu CSCI 240 Analysis of Algorithms Introduction using Insertion Sort

2 Dale Roberts 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?

3 Dale Roberts Example: sorting numbered cards 23 17 45 18 12 22 Given some numbered cards. Our aim is to put them into nondecreasing order.

4 Dale Roberts Example: sorting numbered cards 23 17 45 18 12 22 1 2 6 5 4 3

5 Dale Roberts Example: sorting numbered cards 23 17 45 18 12 22 1 2 6 5 4 3 1 2 6 5 4 3

6 Dale Roberts Example: sorting numbered cards 23 17 45 18 12 22 1 2 6 5 4 3 1 2 6 5 4 3

7 Dale Roberts Example: sorting numbered cards 23 17 45 18 12 22 1 2 6 5 4 3 1 2 6 5 4 3

8 Dale Roberts Example: sorting numbered cards 23 17 45 18 12 22 1 2 6 5 4 3 1 2 6 5 4 3

9 Dale Roberts Example: sorting numbered cards 23 17 45 18 12 22 1 2 6 5 4 3 1 2 6 5 4 3

10 Dale Roberts Example: sorting numbered cards 18 12 22 17 23 45 1 2 6 5 4 3 1 2 6 5 4 3

11 Dale Roberts 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.

12 Dale Roberts Insertion Sort in Pseudocode B[1] = A[1] B[1] = A[1] for j = 2 to n for j = 2 to n { i = j - 1 i = j - 1 while 0 < i and A[j] < B[i] while 0 < i and A[j] < B[i] i = i - 1 i = i - 1 for k = j downto i + 2 for k = j downto i + 2 B[k] = B[k-1] B[k] = B[k-1] B[i+1] = A[j] B[i+1] = A[j] } Insertion of jth card Finding the place to insert A[j] Shifting a part of array B Inserting A[j] A is an array of numbers of length n, B is an empty array

13 Dale Roberts 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)

14 Dale Roberts Analysis for Insertion Sort Insertion-Sort(A)CostTimes (Iterations) 1 B[1] = A[1] c1 2 for j = 2 to n { c2 3 i = j - 1 c3 4 while 0 < i and A[j] < B[i] c4 5 i = i - 1 c5 6 for k = j downto i + 2 c6 7 B[k] = B[k-1] c7 8 B[i+1] = A[j] } c8 1 n - 1 n

15 Dale Roberts Insertion Sort Analysis (cont.) Best Case: Array already sorted, for all j. (Linear in n)

16 Dale Roberts Insertion Sort Analysis (cont.) Worst Case: Array in reverse order, Array in reverse order, Note that We are usually interested in the worst-case running time for all j. (quadratic in n)

17 Dale Roberts Acknowledgements Philadephia University, Jordan Nilagupta, Pradondet


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI"

Similar presentations


Ads by Google