Computer Science 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
Efficiency of Algorithms
Computer Science 101 Efficiency and Complexity Analysis.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 9: Searching, Sorting, and Algorithm Analysis
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Computer Science 112 Fundamentals of Programming II Finding Faster Algorithms.
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.
Efficiency of Algorithms
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
CS107 Introduction to Computer Science
Cmpt-225 Algorithm Efficiency.
Algorithm Efficiency and Sorting
Data Structure Algorithm Analysis TA: Abbas Sarraf
Searching and Sorting Arrays
Elementary Data Structures and Algorithms
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
1 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Analysis of Algorithms CPS212 Gordon College. Measuring the efficiency of algorithms There are 2 algorithms: algo1 and algo2 that produce the same results.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Sorting and Searching Pepper. Common Collection and Array Actions Sort in a certain order ◦ Max ◦ Min Shuffle Search ◦ Sequential (contains) ◦ Binary.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Lecture 10 – Algorithm Analysis.  Next number is sum of previous two numbers  1, 1, 2, 3, 5, 8, 13, 21 …  Mathematical definition 2COMPSCI Computer.
Introduction to Analysis of Algorithms CS342 S2004.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Binary search and complexity reading:
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Searching Topics Sequential Search Binary Search.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Comp 245 Data Structures Analysis of Algorithms. Purpose A professional programmer must be able to determine how efficient his code is. Efficiency, as.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Analysis of Algorithms
Introduction to Search Algorithms
CSC 222: Object-Oriented Programming
Fundamentals of Programming II Finding Faster Algorithms
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Analysis of Algorithms
Computer Science 112 Fundamentals of Programming II
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
Algorithm design and Analysis
CSc 110, Spring 2017 Lecture 39: searching.
Analysis of Algorithms
Fundamentals of Python: First Programs Second Edition
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Algorithmic complexity
Analysis of Algorithms
Presentation transcript:

Computer Science 112 Fundamentals of Programming II Searching, Sorting, and Complexity Analysis

Things to Desire in a Program Correctness Robustness Maintainability Efficiency

Measuring Efficiency Empirical - use clock to get actual running times for different inputs Problems: –Different machines have different running times –Some running times are so long that it is impractical to check them

Measuring Efficiency Analytical - use pencil and paper to determine the abstract amount of work that a program does for different inputs Advantages: –Machine independent –Can predict running times of programs that are impractical to run

Complexity Analysis Pick an instruction that will run most often in the code Determine the number of times this instruction will be executed as a function of the size of the input data Focus on abstract units of work, not actual running time

Example: Search for the Minimum def ourMin(lyst): minSoFar = lyst[0] for item in lyst: minSoFar = min(minSoFar, item) return minSoFar We focus on the assignment ( = ) inside the loop and ignore the other instructions. for a list of length 1, one assignment for a list of length 2, 2 assignments. for a list of length n, n assignments

Big-O Notation Big-O notation expresses the amount of work a program does as a function of the size of the input O(N) stands for order of magnitude N, or order of N for short Search for the minimum is O(N), where N is the size of the input (a list, a number, etc.)

Common Orders of Magnitude ConstantO(k) LogarithmicO(log 2 n) LinearO(n) QuadraticO(n 2 ) ExponentialO(k n )

Graphs of O(n) and O(n 2 )

Common Orders of Magnitude n O(log 2 n) O(n) O(n 2 )O(2 n ) digits yikes!

Suppose an algorithm requires exactly 3N + 3 steps As N gets very large, the difference between N and N + K becomes negligible (where K is a constant) As N gets very large, the difference between N and N / K or N * K also becomes negligible Use the highest degree term in a polynomial and drop the others (N 2 – N)/2  N 2 Approximations

Example Approximations n O(n) O(n) + 2 O(n 2 ) O(n 2 ) + n

def ourIn(target, lyst): for item in lyst: if item == target: return True # Found target return False # Target not there Example: Sequential Search Which instruction do we pick? How fast is its rate of growth as a function of n? Is there a worst case and a best case? An average case?

Improving Search Assume data are in ascending order Goto midpoint and look there Otherwise, repeat the search to left or to right of midpoint target 3 midpoint leftright

Improving Search Assume data are in ascending order Goto midpoint and look there Otherwise, repeat the search to left or to right of midpoint target 5 midpoint rightleft

Example: Binary Search def ourIn(target, sortedLyst): left = 0 right = len(sortedLyst) - 1 while left <= right: midpoint = (left + right) // 2 if target == sortedLyst[midpoint]: return True elif target < sortedLyst[midpoint]: right = midpoint - 1 else: left = midpoint + 1 return False

Analysis while left <= right: midpoint = (left + right) // 2 if target == sortedLyst[midpoint]: return True elif target < sortedLyst[midpoint]: right = midpoint - 1 else: left = midpoint + 1 How many times will == be executed in the worst case?

Sorting a List sort

Selection Sort For each position i in the list –Select the smallest element from i to n - 1 –Swap it with the ith one

Trace smallest i Step 1: find the smallest element i Step 2: swap with first element i Step 3: advance i and goto step 1

for each i from 0 to n - 1 minIndex = minInRange(lyst, i, n) if minIndex != i swap(lyst, i, minIndex) Design of Selection Sort minInRange returns the index of the smallest element swap exchanges the elements at the specified positions

def selectionSort(lyst): n = len(lyst) for i in range(n): minIndex = minInRange(lyst, i, n) if minIndex != i: swap(lyst, i, minIndex) Implementation

def minInRange(lyst, i, n): minValue = lyst[i] minIndex = i for j in range(i, n): if lyst[j] < minValue: minValue = lyst[j] minIndex = j return minIndex def selectionSort(lyst): n = len(lyst) for i in range(n): minIndex = minInRange(lyst, i, n) if minIndex != i: swap(lyst, i, minIndex)

Implementation def swap(lyst, i, j): lyst[i], lyst[j] = lyst[j], lyst[i] def minInRange(lyst, i, n): minValue = lyst[i] minIndex = i for j in range(i, n): if lyst[j] < minValue: minValue = lyst[j] minIndex = j return minIndex def selectionSort(lyst): n = len(lyst) for i in range(n): minIndex = minInRange(lyst, i, n) if minIndex != i: swap(lyst, i, minIndex)

Analysis of Selection Sort The main loop runs approximately n times Thus, the function minInRange runs n times Within the function minInRange, a loop runs n - i times

Analysis of Selection Sort Overall, the number of comparisons performed in function minInRange is n n n = (n 2 – n) / 2  n 2

For Thursday Finding Faster Algorithms