• :00Days
  • :00Hours
  • :00Mins
  • 00Seconds
A new era for learning is coming soonSign up for free
Log In Start studying!

Select your language

Suggested languages for you:
Answers without the blur. Sign up and see all textbooks for free! Illustration

Q7RE

Expert-verified
Discrete Mathematics and its Applications
Found in: Page 232
Discrete Mathematics and its Applications

Discrete Mathematics and its Applications

Book edition 7th
Author(s) Kenneth H. Rosen
Pages 808 pages
ISBN 9780073383095

Answers without the blur.

Just sign up for free and you're in.

Illustration

Short Answer

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

Binary: Ologn

c.) No

See the step by step solution

Step by Step Solution

Step 1: 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)

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

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 On

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 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 are log2n+1 comparisons, while log2n+1 is Ologn

Step 3: 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.

Recommended explanations on Math Textbooks

94% of StudySmarter users get better grades.

Sign up for free
94% of StudySmarter users get better grades.