Topic 1: Problem Solving

Slides:



Advertisements
Similar presentations
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Advertisements

Chapter 9: Searching, Sorting, and Algorithm Analysis
Recursion Chapter 11 Chapter 11.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Searching/Sorting Introduction to Computing Science and Programming I.
Searching Arrays Linear search Binary search small arrays
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.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
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 UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
1 2. Program Construction in Java. 2.8 Searching.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
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.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
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.
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.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Searching Arrays Linear search Binary search small arrays
Algorithms.
Searching and Sorting Arrays
Searching and Sorting Algorithms
Recursion Version 1.0.
Analysis of Algorithms
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Announcements Final Exam on August 17th Wednesday at 16:00.
Types of Algorithms.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Linear and Binary Search
Adapted from Pearson Education, Inc.
Last Class We Covered Data representation Binary numbers ASCII values
Algorithm design and Analysis
searching Concept: Linear search Binary search
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Topic 1: Problem Solving
Standard Version of Starting Out with C++, 4th Edition
Coding Concepts (Basics)
Searching: linear & binary
Coding Concepts (Data- Types)
Linear Search Binary Search Tree
Problem Solving Designing Algorithms.
Data Structures Sorted Arrays
Basics of Recursion Programming with Recursion
Search,Sort,Recursion.
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Topic 1: Problem Solving Searching Algorithms

Problem Solving: Searching Algorithms We’ve seen two important Sorting Algorithms Which take a collection of values And order them (typically from least to greatest) These were the Bubble Sort and the Merge Sort This time we’re going to look at searching algorithms These types of algorithms solve one particular problem Is a specific value in this collection? Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms There may come a time when we need to search for a particular value It doesn’t have to be a number, though that is the common example We could be searching for A person’s name in a register A scheduled event on a calendar An ID in a table of a database Problem Solving: Searching Algorithms

Searching Algorithms We are going to look at two specific algorithms in this topic The Linear Search runs through the collection in a simple, one-way direction The Binary Search removes half of the collection as needed to ‘filter down’ to a single value Although the Linear Search is simpler, the Binary Search takes less time to complete Click here for a good online visualiser for both search algorithms Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Linear Search The Linear Search algorithm itself is simple, considering what we’ve looked at so far All it’s doing is, starting at the beginning of the collection, looking at each element one-by- one If it finds the element we’re looking for, we’re done Otherwise we move on to the next element If we finish looking through the whole collection, then the value we’re looking for is not in the collection BEGIN LinearSearch(nums, search) FOR EACH number IN nums IF number = search RETURN TRUE END IF END FOR RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Linear Search One important note to make about this algorithm We can either make it return True or False OR the index of the found value A simple yes/no answer is handled by the first option If we want to do something with the found value, then the second option would be needed BEGIN LinearSearch(nums, search) FOR EACH number IN nums IF number = search RETURN TRUE END IF END FOR RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Linear Search At the moment this algorithm returns a yes or no value We can easily change it to return an index instead Can you guess how we do this? BEGIN LinearSearch(nums, search) FOR EACH number IN nums IF number = search RETURN TRUE END IF END FOR RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Linear Search We simply use a regular for-loop instead Which means creating a counter for the index We can then return that index If the current value is what we’re looking for If we get past all the values, we know the value isn’t there So we can return -1 As -1 is not a valid index BEGIN LinearSearch(nums, search) FOR index FROM 0 TO SIZE(nums) - 1 IF nums[index] = search RETURN index END IF END FOR RETURN -1 END LinearSearch Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Implement the Linear Search algorithm in your programming language of choice Create two versions of it One returning true/false The other returning the index of the item Don’t forget, return -1 if it wasn’t found BEGIN LinearSearch(nums, search) FOR EACH number IN nums IF number = search RETURN TRUE END IF END FOR RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Binary Search The Binary Search algorithm works a little bit differently Rather than going from the beginning to the end of the collection (like Linear Search) It starts in the middle It then compares the middle against the value we’re looking for If the middle is too big, we remove the upper half of the collection If the middle is too small, we remove the lower half of the collection Either way, we remove half the values and repeat Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Binary Search Here is the pseudocode for this algorithm We start by making values for the lower and upper limits These are indexes They will move as we ‘remove’ half the values From these, we can calculate the mid The value we’ll compare the search against BEGIN LinearSearch(nums, search) lower  0 upper  SIZE(nums) – 1 WHILE lower <= upper mid  (lower + upper) / 2 IF mid = search RETURN TRUE ELSE IF mid > search upper  mid – 1 ELSE lower  mid + 1 END IF END WHILE RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Binary Search Once we’ve got the mid, we compare it against the search If it is too big, we move the upper down If it is too small, we move the lower up If the mid equals the search, then we’ve found the value If the lower is ever larger than the upper, we know we’ve run out of values to look at Meaning the search isn’t in the collection BEGIN LinearSearch(nums, search) lower  0 upper  SIZE(nums) – 1 WHILE lower <= upper mid  (lower + upper) / 2 IF mid = search RETURN TRUE ELSE IF mid > search upper  mid – 1 ELSE lower  mid + 1 END IF END WHILE RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Binary Search The most important thing to remember about this algorithm is… It REQUIRES the values to be IN ORDER That means the collection need to be sorted before we can use Binary Search BEGIN LinearSearch(nums, search) lower  0 upper  SIZE(nums) – 1 WHILE lower <= upper mid  (lower + upper) / 2 IF mid = search RETURN TRUE ELSE IF mid > search upper  mid – 1 ELSE lower  mid + 1 END IF END WHILE RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms

Problem Solving: Searching Algorithms Implement the Binary Search algorithm Remember to sort the array first Make two versions One returns true/false The other returns the index BEGIN LinearSearch(nums, search) lower  0 upper  SIZE(nums) – 1 WHILE lower <= upper mid  (lower + upper) / 2 IF mid = search RETURN TRUE ELSE IF mid > search upper  mid – 1 ELSE lower  mid + 1 END IF END WHILE RETURN FALSE END LinearSearch Problem Solving: Searching Algorithms