Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
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.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Data Structures Using Java1 Chapter 8 Search Algorithms.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
Searching Arrays Linear search Binary search small arrays
Elementary Data Structures and Algorithms
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Chapter 8 ARRAYS Continued
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.
Data Structures Using Java1 Chapter 8 Search Algorithms.
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.
Data Structures Using C++1 Chapter 9 Search Algorithms.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
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.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
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.:
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 (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
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.
Arrays.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
PRESENTATION ON SEARCHING SEARCHING Searching is the method to find element from the list of the elements. If element is found then it.
1 Data Structures Searching Techniques Namiq Sultan.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
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.
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 Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Data Structure Introduction.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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 Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
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.
Data Structures Using C++
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.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Review 1 Insertion Sort Insertion Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
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.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Arrays Department of Computer Science. C provides a derived data type known as ARRAYS that is used when large amounts of data has to be processed. “ an.
Sorting and Searching Bubble Sort Linear Search Binary Search.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Searching Arrays Linear search Binary search small arrays
Search Algorithms Sequential Search (Linear Search) Binary Search
Linear and Binary Search
Searching and Sorting Arrays
Data Structures: Searching
Presentation transcript:

Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب

Searching Searching is a process of checking and finding an element from a list of elements. Let A be a collection of data elements, i.e., A is a linear array of say n elements. If we want to find the presence of an element “data” in A, then we have to search for it. The search is successful if data does appear in A and unsuccessful if otherwise. There are several types of searching techniques; one has some advantage(s) over other. Following are the three important searching techniques: 1. Linear or Sequential Searching 2. Binary Searching 3. Binary Search Algorithm 3. Hashing

Linear or Sequential Searching In linear search, each element of an array is read one by one sequentially and it is compared with the desired element. A search will be unsuccessful if all the elements are read and the desired element is not found.

Algorithm Let A be an array of n elements, DATA is the element to be searched. Then this algorithm will find the location “LOC” of DATA in A. Set LOC = – 1,if the search is unsuccessful. 1. Input an array A of n elements and DATA to be searched and initialize LOC = – Initialize i = 0; and repeat through step 3 while i < n by incrementing i by one. 3. Check if DATA = A[i] then (a) LOC = i (b) GOTO step 4 4. If (LOC != 0) (a) Display “data is found and searching is successful” 5. Else (a) Display “data is not found and searching is unsuccessful” 6. Exit

Time Complexity of Linear Search Time Complexity of the linear search is found by number of comparisons made in searching a record. Best Case In the best case, the desired element is present in the first position of the array, i.e., only one comparison is made. So f (n) = O(1). Average Case In the Average case, the desired element is found in the half position of the array, then f (n) = O[(n )/2]. Worst Case But in the worst case the desired element is present in the nth (or last) position of the array, so n comparisons are made. So f (n) = O(n ).

Binary Search Binary search is an extremely efficient algorithm when it is compared to linear search. Binary search technique searches “data” in minimum possible comparisons. Suppose the given array is a sorted one, otherwise first we have to sort the array elements. Then apply the following conditions to search a “data”.

Algorithm Let A be an array of n elements “Data” is an element to be searched. “MID” denotes the middle location array A. LB and UB is the lower and upper bound of the array which is under consideration. 1. Input an array A of n elements and “DATA” to be searched 2. LB = 0, UB = n; mid = int ((LB+UB)/2) 3. Repeat while (LB <= UB) and (A[mid] ! = data) 4. If (data < A[mid]) (a) UB = mid 5. Else (a) LB = mid 6. Mid = int ((LB + UB)/2) 7. If (A[mid] = = data) (a) Display “the data found” 8. Else (a) Display “the data is not found” 9. Exit

Suppose we have an array of 7 elements Following steps are generated if we binary search a data = 45 from the above array. Step 1: LB = 0; UB = 6 mid = (0 + 6)/2 = 3 A[mid] = A[3] = 30

Step 2: Since (A[3] < data) - i.e., 30 < 45 - reinitialize the variable LB, UB and mid LB = 3 UB = 6 mid = (3 + 6)/2 = 4 A[mid] = A[4] = 40 Step 3: Since (A[4] < data) - i.e., 40 < 45 - reinitialize the variable LB, UB and mid LB = 4 UB = 6 mid = (4 + 6)/2 = 5 A[mid] = A[5] = 45 Step 4: Since (A[5] = = data) - i.e., 45 = = 45 - searching is successful.

Time Complexity Time Complexity is measured by the number f (n) of comparisons to locate “data” in A, which contain n elements. Observe that in each comparison the size of the search area is reduced by half. Hence in the worst case, at most log 2 n comparisons required. Time Complexity in the average case is almost approximately equal to the running time of the worst case.

Searching using Binary Search Tree We assume that a key and the sub tree in which the key is searched for are given as an input. We'll take the full advantage of the BST-property  Suppose we are at a node.  If the node has the key that is being searched for, then the search is over.  Otherwise, the key at the current node is either strictly smaller than the key that is searched for or strictly greater than the key that is searched for.

Searching Algorithm - BST BST-Search(Root, k) 1: Curr Root 2: while Curr != nill do 3: if Curr-> data = k then return Found 4: else if (Curr->data right,k) 5: else found ( Curr->left, k) else 6: return ( FOUND)

C++ Implementation bool search(Node * root, int id) { bool found = false; Nod* Curr=root; if(root == NULL) return false; if(root->data == id) { cout<<" The node is found "<<endl; found = true; Break; } if(!found & id>root->data) found = search(curr->right, id); if(!found& id data) found = search(curr->left, id); return found; }

Time Complexity For n elements Best Case : O(1) Average Case : O(log 2 n) Binary search tree reduce the searching time to half,