Download presentation
Presentation is loading. Please wait.
Published bySybil Russell Modified over 8 years ago
1
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By
2
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.
3
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
4
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.
5
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
6
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)
7
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
8
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.
9
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
10
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 ]) }
11
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
12
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
13
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.