Question

In: Computer Science

Recall the linear search algorithm: procedure linear search (x: integer, a1, a2, ..., an: distinct integers)...

Recall the linear search algorithm:

procedure linear search (x: integer, a1, a2, ..., an: distinct integers)

i := 1

while (i ≤ n and x 6= ai) i := i + 1 if i ≤ n then location:= i else location:= 0 return location

Apply the linear search algorithm to search for the number 3 in the list 1, 5, 3, 9.

(a) In this application of the algorithm, what is the integer n?

(b) What is the initial value of i?

(c) How many iterations of the while loop occur when applied to this list?

(d) What is the value of i after the while loop finishes its iterations?

(e) What does the algorithm return?

Solutions

Expert Solution

(a) In this application of the algorithm, what is the integer n?

>> n is the number of items in the list. So, n = 4(as per the list 1,5,3,9)

(b) What is the initial value of i?

>> 1

(c) How many iterations of the while loop occur when applied to this list?

>> 3

(d) What is the value of i after the while loop finishes its iterations?

>> 3

(e) What does the algorithm return?

>> location=3

Linear search is a sequential search. It will search through every element in the list.

For the list 1, 5, 3, 9,

n=4

i=1

x is the item being searched. So, here, x = 3

tracing while loop:

i=1 i<=4, True

x=ai?

=> x = a1 ?

=> x = 1 ?

=> 3 = 1 ? False

i=i+1=2

i=2 i<=4, True

=> x = a2 ?

=> x = 5 ?

=> 3 = 5 ? False

i=i+1=3
i=3 i<=4, True

=> x = a3 ?

=> x = 3 ?

=> 3 = 3 ? True

-

At this stage the number is found, so i = 3. So, here i<=n which is 3<=4? True and hence it will return location as i which is 3. So, the location returned is 3 which seems to be true as 3 is in 3rd position in the list 1, 5, 3, 9.


Related Solutions

Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . ,...
Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . , an which are known to be bounded by n 2 (so ai ≤ n 2 , for every i = 1, . . . , n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Illustrate your algorithm by showing on paper similar to Fig. 8.3, page 198 in the textbook (make sure you indicate clearly...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose sum is 2n − 2. Prove that there exists a tree T on n vertices whose vertices have degrees a1, a2, . . . , an. Sketch of solution: Prove that there exist i and j such that ai = 1 and aj ≥ 2. Remove ai, subtract 1 from aj and induct on n.
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces...
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces as output a matrix ?={???}M={mij} where ???mij is the minim term in the sequence of integers ??,??+1,…,??ai,ai+1,…,aj for ?≥?j≥i and ???=0mij=0 otherwise. for i := 1 to n for j := 1+1 to n for k:= i+1 to j m[i][j] := min(m[i][j], a[k]) end for end for end for return m a.) Show that this algorithm uses ?(?3)O(n3) comparisons to compute the matrix M....
Let f: X→Y be a map with A1, A2⊂X and B1,B2⊂Y (A) Prove f(A1∪A2)=f(A1)∪f(A2). (B) Prove...
Let f: X→Y be a map with A1, A2⊂X and B1,B2⊂Y (A) Prove f(A1∪A2)=f(A1)∪f(A2). (B) Prove f(A1∩A2)⊂f(A1)∩f(A2). Give an example in which equality fails. (C) Prove f−1(B1∪B2)=f−1(B1)∪f−1(B2), where f−1(B)={x∈X: f(x)∈B}. (D) Prove f−1(B1∩B2)=f−1(B1)∩f−1(B2). (E) Prove f−1(Y∖B1)=X∖f−1(B1). (Abstract Algebra)
Translate the phrases to a system of linear equations: y= a1*x+b1 and y=a2*x+b2 with a variables,...
Translate the phrases to a system of linear equations: y= a1*x+b1 and y=a2*x+b2 with a variables, e.g. x and y. What do the y-intercepts represent in your example? What does the solution (or intersection) represent in your example? Solve the system of equation for x and y.
write a Linear Search algorithm using sentinel approach? How can the sentinel linear search improve the...
write a Linear Search algorithm using sentinel approach? How can the sentinel linear search improve the performance over the standard linear search?
Question in graph theory: 1. Let (a1,a2,a3,...an) be a sequence of integers. Given that the sum...
Question in graph theory: 1. Let (a1,a2,a3,...an) be a sequence of integers. Given that the sum of all integers = 2(n-1) Write an algorithm that, starting with a sequence (a1,a2,a3,...an) of positive integers, either constructs a tree with this degree sequence or concludes that none is possible.
Jojo is given N integers, A1, A2, ..., AN by his teacher. His teacher also give...
Jojo is given N integers, A1, A2, ..., AN by his teacher. His teacher also give him 2M integers, L1,L2,...,LM and R1,R2,...,RM. For each i from 1 to M, his teacher asked him to calculate the sum of odd index numbers from index Li to Ri. For example if Li = 3 and Ri = 7, then he has to calculate the value of (A3 + A5 + A7). Help him by making the program to calculate it quickly! Format...
procedure Example is X: Integer; procedure First is begin Put(X); -- Print X New_Line; -- Print...
procedure Example is X: Integer; procedure First is begin Put(X); -- Print X New_Line; -- Print a newline character end First; procedure Second is X: integer; procedure Third is X: Integer; begin -- Third x := 99; First; end Third; begin -- Second x := 88; Third; end Second; begin -- Example x := 77; First; Second; end Example; a) What will be printed by this program if we assume static scope? b) What will be printed by this program...
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer....
Let A[1..n] be an array of distinct positive integers, and let t be a positive integer. (a) Assuming that A is sorted, show that in O(n) time it can be decided if A contains two distinct elements x and y such that x + y = t. (b) Use part (a) to show that the following problem, re- ferred to as the 3-Sum problem, can be solved in O(n2) time: 3-Sum Given an array A[1..n] of distinct positive integers, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT