0% found this document useful (0 votes)
136 views3 pages

Search Algorithms and Complexity Analysis

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views3 pages

Search Algorithms and Complexity Analysis

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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, [Link]

 (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 [Link]


(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

Common questions

Powered by AI

Linear search is preferred when the list is unsorted, as binary search requires a sorted list to operate correctly . In situations where the overhead of sorting a list for a single search is too high, or where the list frequently changes, linear search becomes more practical.

When performing a sequential search for a non-existent key like 13 in the ordered list [3, 5, 6, 8, 11, 12, 14, 15, 17, 18], 7 comparisons are needed . The search must check each element until the list ends or a match is found.

Big O notation is useful because it provides a way to describe the upper bound performance or worst-case scenario of an algorithm's efficiency in terms of time (or space), allowing for a comparative analysis of algorithm efficiency independent of machine or runtime conditions .

Using a method with poorer worst-case complexity might be appropriate when specific constraints, like memory limitations or dynamic data input reducing overhead through less complexity in setup (like sorting), make it more efficient in practice than theoretically optimal algorithms .

To find the first occurrence of the key 18, two comparisons are necessary, as 18 is found as the second element in the list .

In the worst case, binary search would require examining no more than 8 names . This is approximately log2(193), which rounds up to 8.

Binary search requires fewer comparisons because it eliminates half of the remaining elements in each step by leveraging the sorted nature of the dataset, effectively reducing the problem size logarithmically (O(log N)), unlike linear search which checks each element one by one (O(N)).

The sequence of comparisons for searching the key 16 is 12, 17, 15 . The search proceeds by selecting the middle element 12, then 17, and finally checking 15.

The sequence of comparisons made is 12, 6, 11, 8 . The initial midpoint is 12, leading to a search on the left side with midpoint 6, then 11, and finally narrowing down to the correct key 8.

The big O notations are as follows: For 4N^2 + 2N + 2, the dominant term is N^2, so the time complexity is O(N^2). For N + log N, N is the dominant term, so the time complexity is O(N). For N log N, the time complexity is directly O(N log N). For a constant, such as 3, the time complexity is O(1).

You might also like