0% found this document useful (0 votes)
288 views

Exercise 4: (Choice A)

The document discusses linear and binary search algorithms. It provides examples of functions that implement linear and binary search to find a target element in a list. Binary search is more efficient than linear search when the list is sorted, as it reduces the number of comparisons needed by repeatedly dividing the search space in half.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
288 views

Exercise 4: (Choice A)

The document discusses linear and binary search algorithms. It provides examples of functions that implement linear and binary search to find a target element in a list. Binary search is more efficient than linear search when the list is sorted, as it reduces the number of comparisons needed by repeatedly dividing the search space in half.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Exercise 4

1. Write a function which implements linear search. It should take a list and an
element as a parameter, and return the position of the element in the list. If the
element is not in the list, the function should raise an exception. If the element is
in the list multiple times, the function should return the first position.

Exercise 5

1. Write a function which implements binary search. You may assume that the input
list will be sorted. Hint: this function is often written recursively.

Exercise 6

1. We can see from the comparison tables above that binary search is more
efficient than linear search. Why would we ever use linear search? Hint: what
property must a list have for us to be able to use a binary search on it?
2. Suppose that each of the following functions shows the average number of
operations required to perform some algorithm on a list of length N. Give the big
O notation for the time complexity of each algorithm:
1. 4N2 + 2N + 2
2. N + log N
3. N log N
4. 3

32 teams qualified for the 2014 World Cup. If the names of the teams were arranged in sorted
order (an array), how many items in the array would binary search have to examine to find the
location of a particular team in the array, in the worst case?

 (Choice A)
A
At most, 32.

 (Choice B)

B
At most, 1.

 (Choice C)

C
At most, 6.ans

 (Choice D)

D
At most, 16.

In 2013, there were 193 member states in the United Nations. If the names of these states were sorted
alphabetically in an array, about how many names would binary search examine to locate a particular name
in the array, in the worst case?

Choose 1 answer:
Choose 1 answer:

(Choice A)

A
No more than 193.


(Choice B)

B
No more than 64.


(Choice C)

C
No more than 4.


(Choice D)

D
No more than 8.ans


(Choice E)

E
No more than 128.
Q-40: Suppose you have the following sorted list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18] and are using
the recursive binary search algorithm. Which group of numbers correctly shows the sequence of
comparisons used to find the key 8.
(A) 11, 5, 6, 8
(B) 12, 6, 11, 8 ans
(C) 3, 5, 6, 8
(D) 18, 12, 6, 8
Check MeCompare me

Q-41: Suppose you have the following sorted list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18] and are using
the recursive binary search algorithm. Which group of numbers correctly shows the sequence of
comparisons used to search for the key 16?
(A) 11, 14, 17
(B) 18, 17, 15
(C) 14, 17, 15
(D) 12, 17, 15 ans
Check MeCompare me

Q-50: Suppose you are doing a sequential search of the list [15, 18, 2, 19, 18, 0, 8, 14, 19, 14].
How many comparisons would you need to do in order to find the key 18?
(A) 5
(B) 10
(C) 4
(D) 2 ans
Check MeCompare me

Q-51: Suppose you are doing a sequential search of the ordered list [3, 5, 6, 8, 11, 12, 14, 15,
17, 18]. How many comparisons would you need to do in order to find the key 13?
(A) 10
(B) 5
(C) 7ans
(D) 6
Check MeCompare me

You might also like