Efficiency of Algorithms Csci 107 Lecture 7. Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis.

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
HST 952 Computing for Biomedical Scientists Lecture 9.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
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.
Efficiency of Algorithms February 19th. Today Binary search –Algorithm and analysis Order-of-magnitude analysis of algorithm efficiency –Review Sorting.
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.
Designing Algorithms Csci 107 Lecture 3. Administrativia Lab access –Searles 128: daily until 4pm unless class in progress –Searles 117: 6-10pm, Sat-Sun.
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.
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.
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.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
计算机科学概述 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.
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.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
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 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Sorting.
CMSC 100 Efficiency of Algorithms Guest Lecturers: Clay Alberty and Kellie LaFlamme Professor Marie desJardins Tuesday, October 2, 2012 Some material adapted.
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.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
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.
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.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
16 Searching and Sorting.
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 Csci 107 Lecture 7

Last time –Data cleanup algorithms and analysis –  (1),  (n),  (n 2 ) Today –Binary search and analysis –  (lg n) –Sorting Selection sort

Searching Problem: find a target in a list of values Sequential search –Best-case :  (1) comparison target is found immediately –Worst-case:  (n) comparisons Target is not found –Average-case:  (n) comparisons Target is found in the middle Can we do better? –No…unless we have the input list in sorted order

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… Tuesday: Lab 4 Exam 1 (Wednesday or Monday?) –Material: Algorithms –Chapter 1, 2 & 3 from textbook For next time –Binary search –Review chapter 1,2 & 3 –Practice exam