Consider function search(elm, low, high, myArray) that searches for the value elm in the array myArray between the indexes low and high and returns the index where elm is found in the array or returns -1 if not found.

For example, if myArray is [4, 7, 8, 9, 12, 56], then you have the following:

search(12, 0, 5, myArray) returns 4

search(10, 0, 5, myArray) returns -1

Now consider the linear and binary algorithms below for the function search:

Linear Search

Function search(elm,low,high, myArray)

index = low

While(index <= high)

If myArray[index] == elm then

Return index // found

Else index = index + 1

End While

Return -1 // not found

End Function

Binary Search

Function search(elm,low,high, myArray)

While(high >= low) then

mid = (low + high) / 2

If (myArray[mid] == elm

Return mid; // found

Else

If (myArray[mid] > elm) then // search in the left half

high = mid-1

Else // search in right half

Low = mid +1

End While

Return -1 // not found

End Function

Complete the following:

How many iterations are performed in each algorithm for the function call search(12, 0, 6, myArray) if myArray = [2, 3, 6, 9, 12, 15, 19]?
Which of these two algorithms runs faster?
Why do you believe that one runs faster than the other?

Respuesta :

The iterations that are performed in each algorithm for the function is 5.

Binary search algorithms run faster because of their searching way is based on divide and conquer technique

What is an algorithm?

An algorithm is a limited set of precise instructions that are usually applied to carry out a computation or solve a group of related problems. Calculations and data processing are done according to specifications called algorithms.

Case 1:  Linear Search

The number of Iterations is 5, and the returned index value will be 4,  that is the after 4th Iteration(that is at 5th) the array element will be matched with the searching element

Case 2: Binary Search

The number of Iterations is 3, and the returned index value will be 4,  that is the after 2nd Iteration (that is at 3rd )the array element will be matched with the searching element.

Because there are fewer iterations in the Binary Search algorithm than there are in the Linear Search algorithm, it operates more quickly.

Learn more about algorithm on;

https://brainly.com/question/24953880

#SPJ1