Linear Search Binary Search Tree

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Zabin Visram Room CS115 CS126 Searching
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Arrays Linear search Binary search small arrays
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
COMPSCI 105 S Principles of Computer Science Recursion 3.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Chapter 8 ARRAYS Continued
Building Java Programs Chapter 13 Searching reading: 13.3.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
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)
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Data Structure Introduction.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
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.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved
Copyright Prentice Hall Modified by Sana odeh, NYU
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Using recursion for Searching and Sorting
Recursion Topic 5.
Searching Given a collection and an element (key) to find… Output
Adapted from slides by Marty Stepp and Stuart Reges
Search Algorithms Sequential Search (Linear Search) Binary Search
Applications of Recursion
Teach A level Computing: Algorithms and Data Structures
Searching CSCE 121 J. Michael Moore.
Algorithm design and Analysis
CSc 110, Spring 2017 Lecture 39: searching.
Outline Late Binding Polymorphism via Inheritance
searching Concept: Linear search Binary search
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Searching: linear & binary
CSE 373 Data Structures and Algorithms
24 Searching and Sorting.
Data Structures: Searching
Topic 24 sorting and searching arrays
Revised based on textbook author’s notes.
Adapted from slides by Marty Stepp and Stuart Reges
And now for something completely different . . .
Data Structures and Algorithm: SEARCHING TECHNIQUES
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Sorting and Searching -- Introduction
Sorting Algorithms.
Presentation transcript:

Linear Search Binary Search Tree Revised based on textbook author’s notes.

Searching The process of selecting particular information from a collection of data based on specific criteria. Can be performed on different data structures. sequence search – search within a sequence. search key (or key) – identifies a specific item. compound key – consists of multiple parts.

Linear Search Iterates over the sequence, item by item, until the specific item is found or the list is exhausted. The simplest solution to sequence search problem. Python's in operator: find a specific item. if key in theArray : print( 'The key is in the array.' ) else : print( 'The key is not in the array.' )

Linear Search Examples Searching for 31 Searching for 8

Linear Search Code def linear_search( the_values, target ) : n = len( the_values ) for i in range( n ) : if the_values[i] == target return True # or return I return False # or return -1

Linear Search: Sorted Sequence A linear search can be performed on a sorted sequence. Example: searching for 8.

Linear Search: Sorted Sequence Similar to the unsorted sequence, with one major difference. def sorted_linear_search( the_values, target ) : n = len( the_values ) for i in range( n ) : if the_values[i] == target : return True elif the_values[i] > target : return False

Linear Search: Smallest Value We can search for an item based on certain criteria. Example: find the smallest value. def find_smallest( the_values ): n = len( the_values ) smallest = the_values[0] for i in range( 1, n ) : if the_values[i] < smallest : smallest = the_values[i] return smallest

Binary Search The linear search has a linear time-complexity. We can improve the search time if we modify the search technique itself. Use a divide and conquer strategy. Requires a sorted sequence.

Binary Search Algorithm Examine the middle item (searching for 10): One of three possible conditions: target is found in the middle item. target is less than the middle item. target is greater than the middle item.

Binary Search Algorithm Since the sequence is sorted, we can eliminate half the values from further consideration.

Binary Search Algorithm Repeat the process until either the target is found or all items have been eliminated.

Binary Search Implementation def binary_search( the_values, target ) : low = 0 high = len(the_values) - 1 while low <= high : mid = low + (high - low) // 2 if the_values[mid] == target : return True # or return mid elif target < the_values[mid] : high = mid - 1 else : low = mid + 1 return False # or return -1

Binary Search Implementation

Your Exercise Implement binary search in recursion

Suggested solution def binary_search( the_list, key, low, high ) : if low > high: return False # or return -1 mid = low + (high – low) // 2 if the_list[mid] == key : return True # or return mid elif key < the_values[mid] : return binary_search( the_list, key, low, mid-1 ) else : return binary_search( the_list, key, mid+1, high )