DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

©Silberschatz, Korth and Sudarshan12.1Database System Concepts Chapter 12: Indexing and Hashing Basic Concepts Ordered Indices B+-Tree Index Files B-Tree.
Comp 335 File Structures Indexes. The Search for Information When searching for information, the information desired is usually associated with a key.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Chapter 8 File organization and Indices.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
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.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Binary Trees Chapter 6.
C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods.
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
Indexing. Goals: Store large files Support multiple search keys Support efficient insert, delete, and range queries.
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.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Chapter 2 ARRAYS.
 2006 Pearson Education, Inc. All rights reserved 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. 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 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
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.
Data Structure Introduction.
Chapter 12 B+ Trees CS 157B Spring 2003 By: Miriam Sy.
Storage Structures. Memory Hierarchies Primary Storage –Registers –Cache memory –RAM Secondary Storage –Magnetic disks –Magnetic tape –CDROM (read-only.
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.
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):
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.
Lecture on Binary Search and Sorting. Another Algorithm Example SEARCHING: a common problem in computer science involves storing and maintaining large.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
Marwan Al-Namari Hassan Al-Mathami. Indexing What is Indexing? Indexing is a mechanisms. Why we need to use Indexing? We used indexing to speed up access.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
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.
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.
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.
CS4432: Database Systems II
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 11 Indexing And Hashing (1) Yonsei University 1 st Semester, 2016 Sanghyun Park.
Searching and Sorting Arrays
CHP - 9 File Structures.
Indexing Goals: Store large files Support multiple search keys
Searching and Sorting Arrays
24 Searching and Sorting.
Data Structures: Searching
Advanced Implementation of Tables
Searching and Sorting Arrays
Presentation transcript:

DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING

OVERVIEW At the end of this chapter you’ll be learnt about: – Primary key – Secondary key – Sequential access – Random – Binary search – Linear/Sequential Search

Primary Key vs Secondary Key A primary key is a key or field item that is unique for each record. It is a unique identifier, such as an IC number, matric number, telephone number, and staff number. In the database, it must always have one and only primary key. A choice of primary key, typically depends on the preference of the administrator. It is possible to change the primary key for a given database when the specific needs of the users change

Primary Key vs Secondary Key A secondary key also known as an alternate key, or in SQL may also referred to as a candidate key, is a data field or location that is used for data searches and retrieval

File Access Refers to the process of retrieving data from a file. There are two types of file access: – Random Access – Sequential Access To understand these types of access, u first need to understand some concepts related to how data is organized within a file

File Access: Sequential The term file organization refers to the way data is stored in a file The files we have used, and will continue to use, all have a sequential organization. This means that the characters within the file are stored in a sequential manner, one after another To being sequentially organized, we have also read each file, after it has been opened, in a sequential manner. That is, we have accessed each character sequentially, one after another. This type of access is referred to sequential access

File Access: Random Access In random access, any character in the opened file can be read directly, without first having to sequentially read all characters stored ahead of it To provide random access to files, each ifstream object automatically creates a file position marker. This marker is a long integer that represents an offset from the beginning of each file & keep track of where the next character is to be read from or written to

What is searching? Searching is the operation which finds the location LOC in memory of some given ITEM of information or sends some message that ITEM does not belong to S The searching is said to be successful or unsuccessful according to whether ITEM does or does not belong to S The searching algorithm that is used depends mainly on the type of data structure that is used to maintain S in memory

What is searching? Data modification refers to the operations of inserting, deleting and updating Data modification will mainly refer to inserting & deleting These operations are closely related to searching, since usually one must search for the location of the ITEM to be deleted or one must search for the proper place to insert ITEM in the table It also requires a certain amount of execution time, which also depends mainly on the type of data structure that is used

What is searching? Sorted array – Here, one can use a binary search to find the location LOC of a given ITEM in time O (logn) – On the other hand, inserting & deleting are very slow, since, on the average n/2 = O(n) elements must be moved for a given insertion or deletion – Thus a sorted array would likely be used when there is a great deal of searching but only very little data modification

What is searching? Linked list – Here, one can only perform a linear search to find the location LOC of a given ITEM, and the search may be very slow, possibly requiring time O(n). – On the other hand, inserting and deleting requires only a few pointers to be changed – Thus, a linked list would be used when there is a great deal of data modification, as in word processing

What is searching? Binary Search Tree – This data structure combines the advantages of the sorted array and the linked list – Searching is reduced to search only a certain path P in the tree T, which on the average requires only O(log n) comparisons – Furthermore, the tree T is maintained in memory by a linked representation, so only certain pointers need to be changed after the location of the insertion or deletion is found – The main drawback is that the tree may be very unbalanced, so that length of a path P may be O(n) rather than O(log n). This will reduce the searching to approximately a linear search

Linear Search Each item in the list is examined in the order in which it occurs until the desired item is found or the end of the list is reached This is an analogous of to looking at every name in the phone directory, beginning with Ali, Azahari, until u find the one u want or until u reach Zahara, Zura. Obviously, this is not an efficient way to search a long alphabetized list

Linear Search (cont…)  It has 2 advantages:  The algorithm is simple  The list need not be in any particular order  The search begins at the first item & continues sequentially, item by item, through the list. The pseudocode for a function performing a linear search is: For all the items in the list Compare the item with the desired item If the item was found, Return the index value of the current item EndIf EndFor Return – 1 because the item was not found

Linear Search (cont…) The function linearSearch() illistrates this procedure as C++ function: // this function returns the location of key in the list // a -1 is returned if the value is not found int linearSearch (int list[], int size, int key) { int i; for ( i = 0; i < size; i++) { if ( list [i] == key) return i; } return -1; }

Linear Search (cont…) In reviewing the linearSearch(), notice that the for loop is simply used to access each element in the list, from first element to last, until a match is found with the desired item If the desired item is located, the index value of the current item is returned, which causes the loop to terminate; otherwise the search continues until the end of the list is encountered

Binary Search In binary search, the list must be in sorted order. Starting with an ordered list, the desired item is first compared to the element in the middle of the list (for lists with an even number of elements, either of the 2 middle elements can be used) 3 possibilities present themselves once the comparison is made: – The desired item may be equal to the middle element – It may be greater than the middle element – It may be less than the middle element

Binary Search (cont…)  In the 1 st case, the search has been successful and no further searches are required  In the 2 nd case, because the desired item is greater than the middle element, if it is found at all, it must be in the upper part of the list. This means that the lower part of the list consisting of all the elements from the first to the midpoint element can be discarded from the further search  In the 3 rd case, because the desired item is less than the middle element, if it is found at all, it must be found in the lower part of the list. For this case, the upper part of the list containing all elements from the midpoint to last can be discarded from any further search

Binary Search (cont…)  The pseudocode for binary search: Set the lower index to 0 Set the upper index to 1 less than the size of the list Begin with the first item in the list While the lower index is less than or equal to the upper index Set the midpoint index to the integer average of the lower and upper Compare the desired item to the midpoint element If the desired item equals the midpoint element, Return the index value of current item Else if the item is greater than midpoint, Set the lower index value to the midpoint + 1 Else if the item is less than the midpoint, Set the upper index value to the midpoint – 1 EndIf EndWhile Return -1 because the item was not found

Binary Search (cont…) As illustrated in the pseudocode, a while loop is used to control the search. The initial list is defined by setting the lower index value to 0 & the upper index value to 1 less than the number of elements in the list Once the comparison to midpoint made, the search is subsequently restricted by moving either the lower index to one integer value above the midpoint or by moving the upper index to one integer value below the midpoint The process is continued until the desired element is found

Binary Search (cont…) The function binarySearch() in C++ //this function returns the location of key in the list //a -1 is returned if the value is not found int binarySearch (int list[], int size, int key) { int left, right, midpt; left = 0; right = size -1; while (left <= right) { midpt = (int) ((left + right)/2); if (key == list [midpt]) { return midpt; } else if (key > list [midpt]) left = midpt + 1; else right = midpt – 1; } return 0; }

Summary The primary key is a key or unique item than can represent record, and the secondary key is an alternate key There are 2 common types of file access which are sequential file access and random file access 2 common search techniques: – Linear Search – Binary Search