Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
MATH 224 – Discrete Mathematics
Analysis of Algorithms CS Data Structures Section 2.6.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
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
Elementary Data Structures and Algorithms
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.
ARRAYS, RECORDS AND POINTER
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.
Analysis of Algorithms
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 2 ARRAYS.
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.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Data Strcutures.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
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]
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.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
September 26, 2011 Sorting and Searching Lists. Agenda Review quiz #3 Team assignment #1 due tonight  One per team Arcade game Searching  Linear  Binary.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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 
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.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 1 Searching. Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the.
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.
1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS.
Sorting and Searching Bubble Sort Linear Search Binary Search.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Applied Discrete Mathematics Week 2: Functions and Sequences
Data Structures I (CPCS-204)
Analysis of Algorithms
Arrays.
Linear and Binary Search
CSE 1342 Programming Concepts
Searching and Sorting Arrays
HKOI 2005 Intermediate Training
Searching CLRS, Sections 9.1 – 9.3.
ARRAYS, RECORDS AND POINTER
Data Structures: Searching
Searching and Sorting Arrays
Searching and Sorting Arrays
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Presentation transcript:

Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By

Linear or Sequential Search Let DATA be a list of n elements in memory, location LOC of a given ITEM is to be searched from there. Let DATA be a list of n elements in memory, location LOC of a given ITEM is to be searched from there. The search is said to be successful if ITEM appears in DATA and unsuccessful otherwise. The search is said to be successful if ITEM appears in DATA and unsuccessful otherwise.

Linear or Sequential Search Algorithm of Linear search – Algorithm of Linear search – LINEAR( DATA, n, ITEM, LOC ) LINEAR( DATA, n, ITEM, LOC ) Step 1. Set DATA[n+1] = ITEM Step 1. Set DATA[n+1] = ITEM Step 2. Set LOC=1 Step 2. Set LOC=1 Step 3. Repeat while DATA[LOC] ≠ ITEM Step 3. Repeat while DATA[LOC] ≠ ITEM set LOC=LOC+1 set LOC=LOC+1 end of loop end of loop Step 4. if LOC=n+1 then print “not found” Step 4. if LOC=n+1 then print “not found” Step 5. Return Step 5. Return

Binary Search This search is extremely efficient provided the list to be searched remains sorted. This search is extremely efficient provided the list to be searched remains sorted. Let DATA be a sorted list in increasing numerical order or, equivalently alphabetical order, location LOC of a given ITEM is to be searched from there. Let DATA be a sorted list in increasing numerical order or, equivalently alphabetical order, location LOC of a given ITEM is to be searched from there.

Binary Search Algorithm of Binary search – Algorithm of Binary search – BINARY( DATA, LB, UB, ITEM, LOC ) BINARY( DATA, LB, UB, ITEM, LOC ) Here LB & UB are lower & upper bound resp. The variable used BEG, END & MID denote resp. beginning, end & middle locations of a segment of elements of DATA Here LB & UB are lower & upper bound resp. The variable used BEG, END & MID denote resp. beginning, end & middle locations of a segment of elements of DATA

Binary Search Step 1. Set BEG=LB, END=UB & Step 1. Set BEG=LB, END=UB & MID=INT((BEG+END)/2) MID=INT((BEG+END)/2) Step 2. Repeat Step 3 & Step 4 while Step 2. Repeat Step 3 & Step 4 while BEG<=END & DATA[MID] ≠ ITEM BEG<=END & DATA[MID] ≠ ITEM Step 3. If ITEM<DATA[MID], then set Step 3. If ITEM<DATA[MID], then set END=MID-1 else set BEG=MID+1 END=MID-1 else set BEG=MID+1 end if end if Step 4. Set MID=INT((BEG+END)/2) Step 4. Set MID=INT((BEG+END)/2)

Binary Search Step 5. If DATA[MID]=ITEM, then set Step 5. If DATA[MID]=ITEM, then set LOC=MID else set LOC=NULL LOC=MID else set LOC=NULL end if end if Step 6. Exit Step 6. Exit N.B – This algorithm sets LOC=NULL N.B – This algorithm sets LOC=NULL when ITEM is not found when ITEM is not found

Interpolation Search This search is extremely efficient provided the list to be searched remains uniformly distributed and sorted. This search is extremely efficient provided the list to be searched remains uniformly distributed and sorted. The basic idea of search is that rather than looking at the middle like binary search, look at where the element is expected to be. The basic idea of search is that rather than looking at the middle like binary search, look at where the element is expected to be.

Interpolation Search Algorithm of Interpolation search – Algorithm of Interpolation search – INTERPOLATION ( DATA, LB, UB, ITEM, LOC ) INTERPOLATION ( DATA, LB, UB, ITEM, LOC ) Here LB & UB are lower & upper bound resp. The variable used BEG, END & MID denote resp. beginning, end & expected locations of a segment of elements of DATA Here LB & UB are lower & upper bound resp. The variable used BEG, END & MID denote resp. beginning, end & expected locations of a segment of elements of DATA

Interpolation Search Step 1. Set BEG=LB, END=UB and Step 1. Set BEG=LB, END=UB and MID = MID = { BEG+(ITEM - DATA[BEG]) { BEG+(ITEM - DATA[BEG]) x (END-BEG) ( DATA[END]-DATA[BEG ]) }

Interpolation Search Step 2. Repeat Step 3 & Step 4 while Step 2. Repeat Step 3 & Step 4 while BEG<=END & DATA[MID] ≠ ITEM BEG<=END & DATA[MID] ≠ ITEM Step 3. If ITEM<DATA[MID], then set Step 3. If ITEM<DATA[MID], then set END=MID-1 else set BEG=MID+1 END=MID-1 else set BEG=MID+1 end if end if

Interpolation Search Step 4. Set MID as in Step 1. Step 4. Set MID as in Step 1. Step 5. If DATA[MID]=ITEM, then set Step 5. If DATA[MID]=ITEM, then set LOC=MID else set LOC=NULL LOC=MID else set LOC=NULL end if end if Step 6. Exit Step 6. Exit

Analysis of Searching Complexity of Linear Search - O(n) Complexity of Binary Search - O(log n) Complexity of Interpolation Search - O(log(log n)) N.B-> all log are log 2, for Linear/Sequential Search file may or may not be sorted, for Binary Search file may or may not be sorted, for Binary Search file must be sorted, for Interpolation Search file file must be sorted, for Interpolation Search file must be sorted & uniformly distributed, failure of must be sorted & uniformly distributed, failure of uniformity leads Interpolation search to act as uniformity leads Interpolation search to act as Linear Searching Linear Searching