CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter Trees and B-Trees.
Chapter 4: Trees Radix Search Trees Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Arrays Linear search Binary search small arrays
Chapter 4: Trees Binary Search Trees
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.
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
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,
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Arrays.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
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. 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.
Chapter 9 Searching and Sorting
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
1 Searching the dictionary ADT binary search binary search trees.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Data Structure Introduction.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Visual C++ Programming: Concepts and Projects Chapter 8A: Binary Search (Concepts)
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
1 Lecture 11 b Searching b Linear Search b Binary Search recursiverecursive iterativeiterative polymorphicpolymorphic.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
1 Data Structures CSCI 132, Spring 2014 Lecture23 Analyzing Search Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Chapter 16: Searching, Sorting, and the vector Type.
Searching and Sorting Searching algorithms with simple arrays
Searching – Linear and Binary Searches
Chapter 13: Searching and Sorting
Searching.
Searching and Sorting Arrays
Searching CLRS, Sections 9.1 – 9.3.
Chapter 4.
Applications of Arrays
Presentation transcript:

CHAPTER 11 Searching

2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that it isn't there This requires repetitively comparing the target to candidates in the search pool.

3 Introduction Information: Divided up into records Each record has a key, used in searching Task: Find all records with keys matching the search key Applications: Wide variety – everywhere the data is stored in files How to handle duplicate keys Keep records with distinct keys and link to all other records with the same key Return any record with that key

4 Linear Search A linear search simply examines each item in the search pool, one at a time, until either the target is found or until the pool is exhausted. The items in the search pool are not ordered

5 Linear Search

6 Complexity of Linear Search  (N), O(N) Successful search- about N/2 comparisons Unsuccessful search- N comparisons ( … + N ) = (1 + N)N/2 Average number of comparisons: ( … + N ) / N = (N+1)/2

7 Binary Search Based on the divide-and-conquer strategy. Algorithm Until there are elements in the array repeat: - Divide the array into two parts - Look at the middle element. If it is the sought element – return it. Else - find out to which part the sought record belongs - Repeat he procedure for that part of the file. If no record is found – report unsuccessful search.

8 Binary Search

9 Recursion in Binary Search The Binary search algorithm is recursive. Each recursive call searches a smaller portion of the search pool. The base case of the recursion is running out of viable candidates to search, which means the target is not in the search pool. When the method completes, control returns to the method that invoked it.

10 Complexity of Binary Search  (log(N)), O(log(N)) About Log(N ) comparisons for both successful and unsuccessful search. Why log(N)? Level 0 Level 1 Level 2 Level 3

11 Complexity of Binary Search N nodes at the bottom - the number of elements The root (the node at level 0) corresponds to the whole array When splitting the array into 2, we traverse a path from the root to the bottom nodes. With N nodes at the bottom, the length of the path is log(N). Disadvantage – inserting new records is expensive as the file has to be kept sorted. N/2 records to be moved on average. Duplicate keys - Search both directions

12 Java implementations The Comparable Interface Contains one method, compareTo, which is designed to return an integer that specifies the relationship between two objects: obj1.compareTo(obj2) Returns a number less than, equal to, or greater than 0 if obj1 is less than, equal to, or greater than obj2, respectively public class SortingandSearching