100 likes | 122 Views
Learn about algorithm analysis, insertion sort, and Big-O notation. Explore input size, Selection Sort and Insertion Sort algorithms, and ways to measure algorithm efficiency using Big-O Notation. Get started with your CUNIX account.
E N D
Lecture 3: Analysis of Algorithms • Review • Insertion Sort • Analyzing Algorithms • Big-O Notation • Your CUNIX Account • Reading
Review • Definition of an algorithm: A well-ordered collection of unambiguous and effectively computable operations that, when executed, produces a result and halts in a finite amount of time. • There are three components to an algorithm: • A set of inputs, where each input is a finite sequence of items. • A set of outputs, where each output is a finite sequence of items. • A method consisting of a finite sequence of instructions, each one of which can be mechanically executed in a fixed length of time with a fixed amount of resources. The method must produce an output for each input in the set of possible inputs in a finite amount of time.
Review • Input size: associated with each input is a measure of its size. How we measure the size can vary from problem to problem. Examples: • Number of digits as in the number of digits in a number (Often we use binary digits). • Number of characters in a string. • Number of items as in the number of items to be searched or sorted.
Review: Selection Sort Algorithm to sort the items on a list into nondecreasing order Input: A list of n ≥1 elements A[1], A[2], ... , A[n]. Output: The list sorted in nondecreasing order. Method: for (i = 1; i < n; i++) { locmin=i; for (j=i+1;j <= n; j++) { if (A[locmin] > A[j]) locmin=j; } exchange(A[locmin], A[i]); }
InsertionSort Input: A list of n ≥1 elements A[1], A[2], ... , A[n]. Output: The list sorted in nondecreasing order. Method: for (j=2; j <= n; j ++) { temp = A[j] i=j-1 while (i > 0 and A[i] > temp) { A[i+1]=A[i] i=i-1 } A[i+1]=temp }
Analyzing Algorithms • We would like to measure the quality or efficiency of an algorithm so that we may compare algorithms that perform similar tasks. • We want this measure to be machine (and implementation) independent. • We will consider the number of instructions that need to be executed. • This will be a function of the input size, n. • This may vary depending on “chance”. • We will consider worst-case running time and also average-case running times.
Analyzing Algorithms • Instead of all instructions being counted equal we will count mathematical and/or logical operations involving variables that are unknown ahead of time. • At the end of the day our “measure” will be a function of n. Therefore we need a way of comparing these functions.
Big-O Notation • Given a function of n,we will consider only the most dominant term. What we really care about is the order of magnitude of growth. • Example: T(n)=25n3+log(n)+2n = O(2n) • Example: T(n)=3n2+100n = O(n2) • We’ll save the technical definition for COMS W3203 Discrete Math. The point is we care about growth because we care about really really big n!
Your CUNIX Account • Go to CUIT website at: http://www.columbia.edu/cuit/ • Mac and Windows users download Cyberduck • Windows users also need Putty
Your Codio account • You will receive an email from me with a link to sign up for your Codio account! • These cost the university money so you must be enrolled in the course for this.