StudySmarter AI is coming soon!

- :00Days
- :00Hours
- :00Mins
- 00Seconds

A new era for learning is coming soonSign up for free

Suggested languages for you:

Americas

Europe

Q7RE

Expert-verifiedFound in: Page 232

Book edition
7th

Author(s)
Kenneth H. Rosen

Pages
808 pages

ISBN
9780073383095

**a.) Describe the linear search and binary search algorithm for finding an integer in a list of integers in increasing order.**

** **

**b.) Compare the worst-case time complexities of these two algorithms.**

** **

**c.) Is one of these algorithms always faster than the other (measured in terms of comparisons)?**

**a.) Linear search** searches for the integer by comparing the integer with the first element in the list, then with the second element, then the third and so on. If the element is found, then the algorithm stops and the position of the element is returned.

** **

b.) Linear: $O\left(n\right)$

** **

Binary:** ** $O\left({\mathrm{log}}_{n}\right)$

**c.)** No

**a.) Linear search** searches for the integer by comparing the integer with the first element in the list, then with the second element, then the third so on. If the element is found, then the algorithm stops and the position of the element is returned.

**Binary search** divides the list of integers in two halves. It determines in which half the integer needs to lie (using the increasing order of the initial list) and then the half becomes the new list of the integers. We then divide the list in half again and again determine in which half of the integer lies, which then becomes the new list. This is repeated until the list contains only 1 value. If this value is the integer that we are looking for, the position is returned, else 0 is returned (indicating that the element is not in the list)

**b.) Linear search **in worst case, the integer is not in the list or is the last element in the list, then every element of the list needs to get searched. If the list contains *n* elements, then we thus make *n * comparisons and *n* is $O\left(n\right)$

** **

**Binary search **always executes the algorithm until the list only contains 1 element. Since in every iteration, the list is divide in half, the number of iterations *x* is such that ${2}^{x}\ge n$ and thus $x=\left[{\mathrm{log}}_{2}n\right]$ . Finally 1 more comparison is made (to determine if the remaining element in the list is also the element that we are looking for) and thus in total there are $\u2308{\mathrm{log}}_{2}n\u2309+1$ comparisons, while $\u2308{\mathrm{log}}_{2}n\u2309+1$ is $O\left({\mathrm{log}}_{n}\right)$

c.) One algorithm is not always faster than the other algorithm

For example, if the list contains elements.

Binary search then requires only 3 comparisons (twice dividing in half and then check the remaining element in the list)

However, linear search requires only 1 comparison, if the element for which we are looking is the first element in the list, and linear search requires 4 comparisons, if the element for which we are looking is the last element in the list.

Thus linear search could be faster or slower than binary search.

94% of StudySmarter users get better grades.

Sign up for free