Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Searching and Sorting Topics  Sequential Search on an Unordered File  Sequential Search on an Ordered File  Binary Search  Bubble Sort  Insertion.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Searching and Sorting Topics Sequential Search on an Unordered File
Analysis of Algorithms
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
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.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Lecture on Binary Search and Sorting. Another Algorithm Example SEARCHING: a common problem in computer science involves storing and maintaining large.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Searching Topics Sequential Search Binary Search.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 8A Binary Search (Concepts)
Chapter 16: Searching, Sorting, and the vector Type.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris
Searching and Sorting Arrays
CMPT 120 Topic: Searching – Part 2
CMPT 120 Topic: Searching – Part 2
Introduction to Search Algorithms
CSC 222: Object-Oriented Programming
Introduction to Algorithms
CSc 110, Spring 2017 Lecture 39: searching.
CSC212 Data Structure - Section RS
Searching and Sorting Topics Sequential Search on an Unordered File
Chapter 8 Search and Sort
Searching and Sorting Topics Sequential Search on an Unordered File
Searching and Sorting Arrays
Manipulating lists of data
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting Topics Sequential Search on an Unordered File
Data Structures: Searching
Chapter 4.
CSC212 Data Structure - Section KL
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
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
Applications of Arrays
Relative Rates of Growth
Presentation transcript:

Complexity and Efficiency Computer Science 3 Gerb Objective: Understand complexity and efficiency of algorithms

Complexity and Efficiency Algorithms that run more quickly are said to be more efficient. Algorthims that run less quickly are said to be more complex. For example, consider two algorithms for determining if two strings are equal

Algorithm 1 position = 0, equal=true IF different lengths THEN equal=false ELSE WHILE Position < string length IF characters at position do not match THEN equal=false add one to position RETURN equal

Algorithm 2 position = 0, equal=true IF different lengths THEN equal=false ELSE WHILE Position < string length AND characters at position match Add one to position equal = position >= string length RETURN equal

Algorithm 2 is more efficient Algorithm 1 keeps searching to the end of the string even if the first characters don’t match. –PERFORMANCE –yyyyyynnnnn –PERFORATION Algorithm 2 stops searching when it finds a mismatch. –PERFORMANCE –yyyyyyn –PERFORATION Algorithm 2 is more efficient. Algorithm 1 is more complex.

Another example Example: Find your name in an alphabetically ordered list of N names. Algorithm 1: Start at the first. Keep looking at the next until you find it. Called sequential search. Algorithm 2: Called binary search: Look at the exact middle name IF it’s your name STOP ELSE IF it’s earlier Binary search the 2nd half ELSE Binary search the 1st half

Example: Search for “Jacob” in a list of names Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

Sequential Search: Start at the first name Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

Sequential Search: Progress to the next name until you find Jacob Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

Found! Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

Binary Search: Look at the middle of the list Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston

The one we want is later in the alphabet, so binary search the 2 nd half Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

Look at the middle of the new list Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

Jacob is earlier in alphabetical order, so search the first half of the new list Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

Look at the middle of the new list. We round down in this case Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

Jacob is middle of the new list. Found! Abraham Deborah Evelyn Gabriel Gay Henry Jacob Pamela Paul Peter Winston {

Compare Binary and Sequential Search Algorithm 1 requires looking at an average of N/2 names. Algorithm 2, can double the size of the list, still only need to look at one more name  I.e. requires you to look at Log 2 N names. Algorithm 1 is more complex. Algorithm 2 is more efficient. If N isNeed to look at 11 Name 2-32 Names 4-73 Names Names Names … Names … 1-2 Million21 Names

Summary Complex algorithms take longer than efficient algorithms Sequential search (looking through a list until you find an item) is more complex than binary search (eliminate half the list each time).