Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
Assignment of Algorithms TB

Assignment of Algorithms TB

Ratings: (0)|Views: 354|Likes:
Published by Mukesh Kumar

More info:

Published by: Mukesh Kumar on May 06, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/06/2011

pdf

text

original

 
Subject: AlgorithmsSubject Code: BSIT – 41Assignment: TBQ1. Hand simulate the sequential search algorithm on the data set givenbelow and the search element being 10, 05, 76, 85, 200Data set:: 10, 80, 03, 09, 55, 32, 100, 07, 05, 02, 12, 65, 63, 22, 92, 81,48,76Ans1:Search element: 10Data set:: A[]=↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18i=from subscript 1-18n=no. of elements in the array A[].
The algorithm for sequential search is as follows:
Algorithm:
Sequential search
Input:
A, vector of n elementsK, search elementOutput: j-index of K Method: i=1While (i<n){if(A[i]=K){write(“Search successful”);write(K is at location i)exit();}elsei++if endwhile endWrite(“Search unsuccessful”);algorithm ends(i)Here the search element is
10 an n=18K=10 ←Method:
i=1 (
1
st
pass
)
 
While(i<n) // i.e 1<18 returns true
 1080030955321000705021265632292814876
 
{if(A[i]=K) //hereA[i]=A[1]=10 and K=10 so, it returns true{write(“search successful”)write(K is at location 1)exit();}
Result: K is found in just one pass.Q2. Hand simulate the binary search technique on the given in (Q1) forthe keys.Ans:
Input: A vector of n elements.
Data set:: A[]=↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18Search element: 10
Technique to search:
Binary search. For this method it is necessary tohave the vector in an alphabetical order or numerically increasingorder.
The algorithm for binary search is as follows:
Algorithm:
binary search
Input:
A, vector of n elementsK, search element
Output:
low-index of K 
Method:
low=1 high=nWhile(low<=high-1){mid=(low+high)/2if(K<a[mid])high=midelselow=midif end}while endif(K=A[low]){write(“search successful”)write(K is at location low)exit();}
1080030955321000705021265632292814876
 
elsewrite(“search unsuccessful”)if endsalgorithm ends
Note: So, before hand simulating we need to have it in a numericallyincreasing order.
So the new data set will look as given below.0203050709101222324555636576808192100Subscript from 1-18(i)Search element =10 Number of elements n=18
Method:(1
st
pass)
low=1 , high=n// i.e. high=18While(low<=high-1)//(1<=18-1(=17)) returns true{mid=(1+18)/2 // mid=19/2~9if(K<a[mid])//here K=10 and a[mid]=a[9]=32 returns truehigh=mid //high=9//again moving up to the method
(2
nd
pass)
low=1 , high=9while(1<=9-1) //returns true{ mid=(1+9)/2 //mid=10/2=5//mid=a[5]if(K<a[mid])//K=10 and a[mid]=a[5]=09. Soit returns false so else part will be executedelselow=mid //low=mid=5
(3
rd
pass)
low=5 , high=9While(5<=9-1(=8)) //returns truemid=(5+9)/2 //mid=14/2=7if(10<a[7]) //i.e. (10<12) returns truehigh=mid //high=7
(4
th
pass)
low=5 , high=7While(5<7-1(=6)) //returns true

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->