Call Girls In Nandini Layout ☎ 7737669865 🥵 Book Your One night Stand
4- searching.ppt
1. Searching
• Fundamental operation in data structure
• The process of finding a specific data item or
record from a list is called searching
– Fast searching important task ,other operation
insertion deletion depends on this
– It is successful if the record is found other wise fail
• Different technique are use as
– Sequential search
– Binary search
2. Linear Search
• The Linear Search, or sequential search, is
simply examining each element in a list one by
one until the desired element is found.
– The Linear Search is not very efficient.
• It start from the first element in the list to the last element and
compare the required item with each one by one why it is
called Linear search
– It is a slow process
– Used for small amount of data
• Its algorithm and its implementation is as
3. Algorithm for Linear Search
Algorithm to find a value ITEM from an array ABC
consisting of N elements
1. SET LOC=-1
2. [Enter value that has to be serched]
INPUT ITEM
3. REPEAT STEP 4 FOR I=1 TO N
4. IF ITEM=ABC[I]
LOC=I
PRINT” data found at location ”LOC
EXIT
END IF
[END LOOP]
5. IF LOC=-1
PRINT “item not Found”
exit
END IF
4. Binary Search
• Efficient technique to search a specific item from
a list of items
• Used for large list of data that are sorted in
ascending or descending order
– It searches from the middle of the list , if the list is
sorted in ascending order and the required value is
greater than the value at the middle , the control goes
to the higher value next to middle value that become
min now
– If the required value is less than the middle value then
the control goes to the lesser value previous to middle
value that become max now
– This process is continue till value found
• If value found it is successful otherwise
unsuccessful
5. Algorithm for Binary Search
• Algorithm to find a value ITEM in array ABC
,consist of N elements in sorted order LOC hold
the position of the element that has the required
value , MID variable to hold the position of the
middle of the array
• For this to find MID
– MID =(1+N)/2
– This add first and last script value and find its average
the whole integer script is taken the middle
6. 1. SET LOC=-1
2. [enter value to search]
INPUT ITEM
3. MID=(1+N)/2
4. IF ITEM=ABC[MID] THEN
Loc=MID
PRINT" value found at middle”
EXIT
END IF
5. [search towards right]
If ITEM>ABC[MID]
]THEN
REPEAT FOR I=MID+1 TO N BY 1
IF ITEM=ABC[I] THEN
Loc=I
PRINT “VALUE AT POSITION”,I
EXIT
END IF
ELSE [check toward left]
REPEAT FOR I=MID-1 TO 1 BY -1
IF ITEM=ABC[I] THEN
Loc=I
PRINT” value at position ”,I
END IF
IF LOC=-1 THEN
PRINT “Data not found”
EXIT
END IF