Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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)?

Short Answer

Expert verified

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: On

Binary:Ologn

c.) No

Step by step solution

Achieve better grades quicker with Premium

  • Unlimited AI interaction
  • Study offline
  • Say goodbye to ads
  • Export flashcards

Over 22 million students worldwide already upgrade their learning with Vaia!

01

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

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)

02

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

b.) Linear searchin 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 On

Binary searchalways 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 2xn and thus x=log2n . 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 arelog2n+1 comparisons, whilelog2n+1 is Ologn

03

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

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.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Study anywhere. Anytime. Across all devices.

Sign-up for free