CS114-009 Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

CS4413 Divide-and-Conquer
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
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.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
Quicksort.
This material in not in your text (except as exercises) Sequence Comparisons –Problems in molecular biology involve finding the minimum number of edit.
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 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Quicksort
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Searching Arrays Linear search Binary search small arrays
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
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.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
Chapter 16: Searching, Sorting, and the vector Type.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
CSCI 130 Array Searching. Methods of searching arrays Linear –sequential - one at the time –set does not have to be ordered Binary –continuously cutting.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CSC 211 Data Structures Lecture 13
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Searching Sequential Search Binary Search. Sequential Search Sequential search is to look into the key one after another until the target is found If.
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.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Algorithms
Recitation 13 Searching and Sorting.
Sorting by Tammy Bailey
Adapted from Pearson Education, Inc.
MSIS 655 Advanced Business Applications Programming
24 Searching and Sorting.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Presentation transcript:

CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class  Programming Project #6 due Nov. 20 by midnight to

Programming project #6project #6 Skeleton of program #6program #6

When software goes wrong… What does software do?  Payrolls, bills, businesses…  Phones & other ‘net’ systems  Elevator controls  Anti-lock brake controllers  Insulin pumps, heart pacemakers  X-ray therapy equipment  Aircraft control (“fly by wire”) What happens when software fails?  Bills and paychecks wrong  When phones die…  Elevators…  Auto brakes…  Insulin pumps…  X-ray machines with bad software have killed Therac-25  Aircraft Commercial passenger planes have crashed

How important is it for software to work? If software doesn’t work,  Things go wrong,  Businesses fail,  People lose their jobs,  People are injured,  People even die… Many people have died!

How can we make software work? Careful planning: algorithms, good teaming skills, good communication Teaching those skills Making sure students know what we’re teaching.

A “better” algorithm: Merge Sort Basic idea:  A list of size one is sorted  It is easy to merge sorted lists into a larger sorted list Algorithm  Break list up into lots of small (1 item) lists  Merge these lists  Will discuss later Example

Our basic search technique Assumes array is not sorted On average, takes N/2 tries to find it (if it is there)  Sometimes find it early  Sometimes find it late  On average, will find it half-way through Consider an array of 1,000,000 elements Current technique averages 500,000 comparisons Can do it in 20 earlylateaverage

Efficient Searching Binary Search  Array must be sorted  Compare search value with the middle item Middle larger? Must be in lower (first) half Middle smaller? Must be in the upper (second) half  Examples Looking for 4, first half Looking for 57, last half Basic Algorithm  Find middle item  Compare search value Ignore one half of array  Repeat this process on the other half of the array until you find it (or realize it is not there)

Binary Search Example: find Middle item is 34, less than 55, throw it and everything to the left of it out Repeat: Middle item is 60, greater than 55, throw it and everything to the left of it out Repeat: Middle item is 56, greater than 55, throw it and everything to the right of it out Repeat: Middle item is 55, Found it !

Class Exercises Find 81 Find 5 Find 50 Find 8 How many comparisons does it take to find each one (or realize it is not there)? Initial middle point

How fast is binary search? Quick math review  If log 2 N = K, then N = 2 K If K=5, N = 2 5 = 32 If K=10, N = 2 10 = 1024 If K=20, N = 2 20 = Compare sequential and binary search Why is binary search O(log 2 N) ?  Throw away half of items each time 1,000  500  250  …  4  2  1  Keep discarding half until down to single item How many steps to get to one item (worst case?)  Takes log 2 N steps to get down to one item Complexity is O(log 2 N) N/2, avg sequentiallog 2 N, avg binary 100 / 27 10,000 / ,000 / 217 1,000,000 / 220

Class Exercise Classic guessing game  Let a person pick a number in the range from 1 to You can guess it in ten or fewer attempts. Try it  Use the “binary search” principle: Always guess the mid-point of the remaining numbers  One person picks a number from 1 to 1000  Have the others guess (tell them higher or lower)  How many tries did it take? Note: You can guess 1 – 1,000,000 in 20 attempts.

Review: Binary Search Finds in O(log 2 N) time  N is the size of the data (length of the array) What does this mean?  Array has 1,000 elements 10 steps or less  Array has 1,000,000 items 20 steps or less  Eliminate half each time Recursive solution  Find the midpoint  Compare our number with the midpoint if it’s the same, found it  If our item is less, recursively call binary search on left side from start to mid-1  If our item is greater, recursively call binary search on right side from mid+1 to the end

Our algorithm Function takes four parameters  Array  Lowest point to search  Highest point to search  Item to find Configuration  int array[1000], item;  array is in sorted order  Looking for item Initial call search(array, 0, 999, item)

Our algorithm int search (array [ ], firstIndex, lastIndex, value) { if (firstIndex > lastIndex) item was not in the array mid = (firstIndex+lastIndex) / 2 if (value == array[mid]) found it else if (value > array[mid]) search(array, mid+1, lastIndex, value) else // (value < array[mid]) search(array, firstIndex, mid-1, value) }

Tracing our algorithm int search (array [ ], firstIndex, lastIndex, value) { if (firstIndex > lastIndex) item was not in the array mid = (firstIndex+lastIndex) / 2 if (value == array[mid]) found it else if (value > array[mid]) search(array, mid+1, lastIndex, value) else // (value < array[mid]) search(array, firstIndex, mid-1, value) array [ ] value 73 search(array, 0, 7, 73) first 55, search upper half search(array, 4, 7, 73) first <= last, not done yet mid = (4+7)/2 = 5 array[mid] = 88 value < 88, search lower half search(array, 4, 4, 73) first <= last, not done yet mid = (4+4)/2 = 4 array[mid] = 73 found it (value == array[mid])

Class Exercises int search (array [ ], firstIndex, lastIndex, value) { if (firstIndex > lastIndex) item was not in the array mid = (firstIndex+lastIndex) / 2 cout << mid << endl; if (value == array[mid]) found it else if (value > array[mid]) search(array, mid+1, lastIndex, value) else // (value < array[mid]) search(array, firstIndex, mid-1, value) array [ ] Using this same array, search for Trace the function, notice the cout of mid.  7  93  50  104  42

The actual C++ implementation int search( int array[ ], int first, int last, int key) { if (first > last) return -1; int mid = (first + last) / 2; if (array[mid] == key) return mid; if (array[mid] > key) return search(array, first, mid-1, key); else return search(array, mid+1, last, key); } 1. Takes 4 Parameters 2. Check for “not found” 3. Find midpoint 4. Check midpoint 5. Less than midpoint, look in lower half 6. Greater than midpoint, look in upper half

Class Exercises The file Binary-Search.cpp (on our class web page) has an implementation of binary search.Binary-Search.cpp The program  Uses an array of 10 integers  Prompts you for a search value  Shows the series of calls that are made during the search for this number Download, compile, and run this program  Make sure you understand what it is doing

End of Class 22