Efficiency of Algorithms February 19th. Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting.

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
The Efficiency of Algorithms
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
HST 952 Computing for Biomedical Scientists Lecture 10.
Fundamentals of Python: From First Programs Through Data Structures
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Chapter 9: Searching, Sorting, and Algorithm Analysis
8 Algorithms Foundations of Computer Science ã Cengage Learning.
HST 952 Computing for Biomedical Scientists Lecture 9.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Efficiency of Algorithms
CS107 Introduction to Computer Science
Chapter 3 The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
Efficiency of Algorithms Csci 107 Lecture 8. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
Algorithms and Efficiency of Algorithms February 4th.
Chapter 3: The Efficiency of Algorithms
Designing Algorithms Csci 107 Lecture 4.
Algorithm Efficiency and Sorting
Efficiency of Algorithms Csci 107 Lecture 6. Last time –Algorithm for pattern matching –Efficiency of algorithms Today –Efficiency of sequential search.
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
1 Algorithms and Analysis CS 2308 Foundations of CS II.
Searching and Sorting Arrays
Chapter 3: The Efficiency of Algorithms
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
1 Chapter 3: Efficiency of Algorithms Quality attributes for algorithms Correctness: It should do things right No flaws in design of the algorithm Maintainability.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
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.
Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CSCI 130 Array Searching. Methods of searching arrays Linear –sequential - one at the time –set does not have to be ordered Binary –continuously cutting.
CSC 211 Data Structures Lecture 13
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
Sorting.
ALGORITHMS.
Invitation to Computer Science 6th Edition Chapter 3 The Efficiency of Algorithms.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
16 Searching and Sorting.
Applied Discrete Mathematics Week 2: Functions and Sequences
Introduction to Search Algorithms
Sorting by Tammy Bailey
Algorithm design and Analysis
Chapter 3: The Efficiency of Algorithms
MSIS 655 Advanced Business Applications Programming
Searching and Sorting 1-D Arrays
Chapter 3: The Efficiency of Algorithms
Invitation to Computer Science 5th Edition
Presentation transcript:

Efficiency of Algorithms February 19th

Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting –Selection sort

Searching a sorted list Problem: find a target in a sorted list –How can we exploit that the list is sorted, and come up with an algorithm faster than sequential search in the worst case? –How do we search in a phone book? –Can we come up with an algorithm? Check the middle value If smaller than target, go right Otherwise go left

Binary search Get values for list, A1, A2, ….An, n, target Set start =1, set end = n Set found = NO Repeat until ?? –Set m = middle value between start and end –If target = m then Print target found at position m Set found = YES –Else if target < Am then end = m-1 Else start = m+1 If found = NO then print “Not found” End

Efficiency of binary search What is the best case? What is the worst case? –Initially the size of the list in n –After the first iteration through the repeat loop, if not found, then either start = m or end = m ==> size of the list on which we search is n/2 –Every time in the repeat loop the size of the list is halved: n, n/2, n/4,…. –How many times can a number be halved before it reaches 1?

Orders of magnitude Order of magnitude  ( lg n) –Worst-case efficiency of binary search:  ( lg n) Comparing order of magnitudes  (1) <<  (lg n) <<  (n) <<  (n 2 )

Sorting Problem: sort a list of items into alphabetical or numerical order Why sorting? –Sorting is ubiquitous (very common)!! –Examples: Registrar: Sort students by name or by id or by department Post Office: Sort mail by address Bank: Sort transactions by time or customer name or accound number … For simplicity, assume input is a list of n numbers Ideas for sorting?

Selection Sort Idea: grow a sorted subsection of the list from the back to the front | | | | … |

Selection Sort Pseudocode (at a high level of abstraction) –Get values for n and the list of n items –Set marker for the unsorted section at the end of the list –Repeat until unsorted section is empty Select the largest number in the unsorted section of the list Exchange this number with the last number in unsorted section of list Move the marker of the unsorted section forward one position –End

Selection Sort Level of abstraction –It is easier to start thinking of a problem at a high level of abstraction Algorithms as building blocks –We can build an algorithm from “parts” consisting of previous algorithms –Selection sort: Select largest number in the unsorted section of the list We have seen an algorithm to do this last time Exchange 2 values

Selection Sort Analysis Iteration 1: –Find largest value in a list of n numbers : n-1 comparisons –Exchange values and move marker Iteration 2: –Find largest value in a list of n-1 numbers: n-2 comparisons –Exchange values and move marker Iteration 3: –Find largest value in a list of n-2 numbers: n-3 comparisons –Exchange values and move marker … Iteration n: –Find largest value in a list of 1 numbers: 0 comparisons –Exchange values and move marker Total: (n-1) + (n-2) + …

Selection Sort Total work (nb of comparisons): –(n-1) + (n-2) + … –This sum is equal to.5n 2 -.5n (proved by Gauss) => order of magnitude is  ( ? ) Questions –best-case, worst-case ? –we ignored constants, and counted only comparisons.. Does this make a difference? Space efficiency –extra space ?

Selection Sort In conclusion: Selection sort –Space efficiency: No extra space used (except for a few variables) –Time efficiency There is no best-case and worst-case the amount of work is the same:  (n 2 ) irrespective of the input Other sorting algorithms? Can we find more efficient sorting algorithms?

This week… Wednesday: Lab 3 Thursday: Exam 1 Material: Algorithms Chapter 1, 2 & 3 from textbook For next time –Review chapter 1,2 & 3 –Look over practice exam