Data Structures: Searching

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Linked Lists Linked Lists Representation Traversing a Linked List
CSE Lecture 3 – Algorithms I
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.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
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.
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)
Lecture 12. Searching Algorithms and its analysis 1.
Arrays.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Data Strcutures.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
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 Structures and Algorithms TREE. Searching Sequential Searches Time is proportional to n We call this time complexity O(n) Pronounce this “big oh”
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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 and Searching by Dr P.Padmanabham Professor (CSE)&Director
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Searching Sequential Search Binary Search. Sequential Search Sequential search is to look into the key one after another until the target is found If.
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.
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.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
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.
Sorting and Searching Bubble Sort Linear Search Binary Search.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
FALL 2005CENG 213 Data Structures1 Review. FALL 2005CENG 213 Data Structures2 Collection of items Problems that must manage data by value. Some important.
Sections 8.7 – 8.8 Balancing a Binary Search Tree.
Searching an Array: Binary Search
Chapter 7 Single-Dimensional Arrays
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Search Algorithms Sequential Search (Linear Search) Binary Search
Topic 14 Searching and Simple Sorts
Linear and Binary Search
Chapter 6 Transform and Conquer.
Searching.
CSC215 Lecture Algorithms.
Search Sorted Array: Binary Search Linked List: Linear Search
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
CSE 373 Data Structures and Algorithms
Searching CLRS, Sections 9.1 – 9.3.
Linear Search Binary Search Tree
24 Searching and Sorting.
Topic 14 Searching and Simple Sorts
Topic 24 sorting and searching arrays
Chapter 4.
Data Structures and Algorithms
Searching and Sorting Arrays
CSC 143 Binary Search Trees.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Data Structures and Algorithms
Search Sorted Array: Binary Search Linked List: Linear Search
Data Structures Using C++ 2E
Applications of Arrays
Presentation transcript:

Data Structures: Searching Course teacher:Moona Kanwal

Searching Searching refers to the operation of finding the location LOC of ITEM in DATA Search is said to be successful if ITEM is found in DATA set otherwise search is said to be unsuccessful 2 types of search techniques Linear Search Binary Search

Searching Linear Search Binary search Only used in arrays Time is proportional to n We call this time complexity O(n) Pronounce this “big oh” of n Both arrays (unsorted) and linked lists Binary search Sorted array Time proportional to log2 n Time complexity O(log n) Only used in arrays

Searching – Linear Search Linear search searches ITEM one by one in DATA set. If item is found it returns its location otherwise set LOC to NULL and return If there are N number of elements in DATA Linear search will compare each element of DATA with given ITEM until either search is successful or no more element to compare.

Searching : Binary Search Divide-an-Conquer Elements must be sorted Efficient Searching technique Application Searching in Telephone Directory Working Two pointers are used Begin: which points to 1st element in DATA block End: which points to last element in DATA block

Another pointer MID points to middle value of the DATA block MID is determined as MID = CEIL(BEGIN + END)/2 The given ITEM is compared with DATA[MID] If DATA[MID] = ITEM then LOC = MID and Location is returned Else if DATA[MID] < ITEM As already stated DATA is sorted then if ascending then item if exist in DATA can only be found on left of DATA[MID] There fore we need only to search the ITEM to left of mid value and discard all other elements Thus to do this all we have to do is to move END to point to end of the left portion of DATA[MID] i.e END = MID-1. No changes is required in BEGIN

Else if DATA[MID] > ITEM As already stated DATA is sorted then if ascending then item if exist in DATA can only be found on right of DATA[MID] There fore we need only to search the ITEM to right of mid value and discard all other elements Thus to do this all we have to do is to move BEGIN to point to start of the right portion of DATA[MID] i.e BEGIN = MID+1. No changes is required in END We continue with the search by dividing the DATA block until either search is successful or BEGIN > END at which the search terminates NULL is returned showing that search is unsuccessful

EXAMPLE Data : 11 ,22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 89 ITEM = 40 Initially BEGIN = 1 and END = 13 MID = 7, DATA[MID] = 55 Since 40<55 so END = MID -1 = 7-1 = 6 so new MID = 3, DATA[MID] = 30 Since 40>30 BEGIN = MID + 1=4 so New MID = 5, DATA[MID] = 40 ITEM found so LOC = MID = 5

EXAMPLE cont.. Same data elements ITEM = 85 BEGIN = 1, END = 13, MID = 10, DATA[MID] = 77 85>77 so BEGIN = 10+1 =11 New MID = 12, DATA[MID] = 88 85<88 so END =12-1 =11 New MID = 11, DATA[MID] = 80 So BIGIN = END = MID 85 ≠ 80 ITEM not found

Duplication Issue Linear Search: If ITEM occurs more than one time in DATA then location for each occurrence should be returned. The algorithm for linear search assumes only one occurrence of ITEM

Search with insert and delete Search and Insert The ITEM is searched by using any technique appropriate to given DATA If it is not found ITEM is inserted at the end of DATA Search and Delete If it is found ITEM is deleted from the DATA If more than one time ITEM is found than all occurrence are deleted