0% found this document useful (0 votes)
15 views12 pages

Searching

Uploaded by

Ddevil from hel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Searching

Uploaded by

Ddevil from hel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Searching

Two Basic Searches for arrays


• Sequential Search:
– Can be used to locate an item in any array.
• Binary Search:
– Requires an ordered list.
Sequential/Linear Search
• Used when the list is not ordered.
• Used for small lists or lists that are not
searched often.
• Algorithm of Linear Search
Algorithm of Linear Search
int linearSearch(int a[], int first, 1. Repeat for i=first to last
int last, int key) a. if (key = a[i])
Searches a[first]..a[last] for key. display “key found at
returns: index of the matching location I”
element if it finds key,
otherwise -1. 2. return -1; // failed to find key
a in array of (possibly unsorted)
values.
first, last in lower and upper
subscript bounds key in value
to search for.
returns:
index of key, or -1 if key is not
in the array.
Algorithm of Linear Search
void LinearSearch(int key)
1. Declare and Initialize
flag =0
2. Repeat a for i=0 to n-1
a. if (key = a[i])
1. display “key found at location “ i+1
2. flag =1

3. If flag =0
Display key not found
Sequential Search
• Used when the list is not ordered.
• Used for small lists or lists that are not
searched often.
• Algorithm of Linear Search
Efficiency of linear search
Model No. of Comparisions as
Comparisions a function of n

Best Case 1(target is the first 1


(fewer comparisions) item)
Worst Case(most n(target is the last n
comparisions) item)
Average Case(Avg n/2(target is the n/2
no. of comparisons) middle item)
Disadvantage of sequential search
• Sequential search is very slow.
• Eg:
Binary Search
• Binary search is used when the list is
sorted.
• It is used whenever the list starts to
become large.(more than 15 elements).
• Working:
Algorithm of Binary Search
binarysearch(int a[],int key, Step 3:
int n) mid=(first+last)/2
//a[]:list of elements Step 4:
//key:the value to be if(key<a[mid])
searched last=mid -1
//n:no. of elements in the list elseif(key>a[mid])
Step 1:[initialize] first=mid+1
first=0 elseif(key==a[mid])
last =n-1 print “search
flag=0 successful”
Step 2: and location of
1.repeat steps 3 and 4 the element mid
while(first<=last) flag=1
Algorithm of Binary Search
Step 5:
if( flag=0)
print “search is
unsuccessful)
Step 6:
Exit
Efficiency of binary search

Model No. of Comparisions as


Comparisions a function of n

Best Case 1(target is the 1


(fewer comparisions) middle item)
Average Case Log2 n

Worst Case(most n(target is the last n


comparisions) item)

You might also like