170 likes | 334 Views
Department of Computer and Information Science, School of Science, IUPUI. CSCI 240. Analysis of Algorithms Introduction using Insertion Sort. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu. Characteristics of Algorithms.
E N D
Department of Computer and Information Science,School of Science, IUPUI CSCI 240 Analysis of Algorithms Introduction using Insertion Sort Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu
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 23 45 22 12 17 18 Given some numbered cards. Our aim is to put them into nondecreasing order.
Example: sorting numbered cards 23 45 22 12 17 18 3 1 6 5 4 2
Example: sorting numbered cards 23 45 22 12 17 18 3 1 6 5 4 2 3 1 6 5 4 2
3 1 6 5 4 2 Example: sorting numbered cards 45 22 12 17 18 3 1 6 5 4 2 23
3 1 6 5 4 2 Example: sorting numbered cards 45 22 12 18 3 1 6 5 4 2 17 23
3 1 6 5 4 2 Example: sorting numbered cards 22 12 18 3 1 6 5 4 2 17 23 45
3 1 6 5 4 2 Example: sorting numbered cards 22 12 3 1 6 5 4 2 17 23 45 18
3 1 6 5 4 2 Example: sorting numbered cards 3 1 6 5 4 2 18 22 45 12 23 17
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.
Finding the place to insert A[j] Insertion of jth card Shifting a part of array B Insertion Sort in Pseudocode A is an array of numbers of length n, B is an empty array B[1] = A[1] for j = 2 to n { i = j - 1 while 0 < i and A[j] < B[i] i = i - 1 for k = j downto i + 2 B[k] = B[k-1] B[i+1] = A[j] } Inserting A[j]
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]c1 2 for j = 2 to n { c2 3 i = j - 1c3 4 while 0 < i and A[j] < B[i]c4 5 i = i - 1c5 6 for k = j downto i + 2c6 7 B[k] = B[k-1]c7 8 B[i+1] = A[j] } c8 1 n n - 1 n - 1
for all j. Insertion Sort Analysis (cont.) • Best Case: Array already sorted, (Linear in n)
for all j. Insertion Sort Analysis (cont.) • Worst Case: Array in reverse order, We are usually interested in the worst-case running time (quadratic in n) Note that
Acknowledgements • Philadephia University, Jordan • Nilagupta, Pradondet