Question

In: Computer Science

Consider the sorted array myList of integers below. 2 5 21 25 35 40 45 48...

Consider the sorted array myList of integers below.


2 5 21 25 35 40 45 48 70 75 89 99


The Binary Search algorithm uses two index variables first and last to determine the position
(stored in another index variable mid) to search for an element. Using Binary Search to search
for the element 75 in myList, give the values of myList[first], myList[mid] and
myList[last] (in the form of a table) every time a new value is calculated for mid.

Solutions

Expert Solution

The values for the first, last, and mid values using the Binary Search Algorithm to search for 75 is as follows:

First Last Mid
2 99 40
45 99 70
75 99 89
75 75 75

The program terminates when mid value=75(the value to be found).

The binary search algorithm computes the middle value and then checks if the value to be found is equal, less, or greater than the value present at the mid index.

If the value is less than mylist[mid], the program updates the last index to mid-1.

If the value is greater than mylist[mid], the program updates the first index to mid+1.

If the value is equal to mylist[mid], the program returns the index.

I'm also attaching a sample code of the Binary Search Algorithm for you to test these values:

#include<bits/stdc++.h> 
#include<iostream>
using namespace std; 

int binary_search(int mylist[], int first, int last, int val)
{
        while(first <= last) { 
          cout<<"first="<<mylist[first] <<" " << "last="<<mylist[last]<<endl;       
  
    int mid=first+(last-first)/2; 
                cout<<"mid="<<mylist[mid] <<endl;
    
                if(mylist[mid]==val) 
                        return mid; 
                if(mylist[mid]<val) 
                        first=mid+1;
                else
                        last=mid-1; 
        } 
        return -1; 
} 

int main() 
{ 
        int mylist[]={ 2 ,5, 21, 25, 35, 40, 45, 48, 70, 75, 89, 99 }; 
        int val=75; 
        int size=sizeof(mylist)/sizeof(mylist[0]); 
        int index=binary_search(mylist,0,size-1,val); 
    if(index==-1)
       cout<<"Element not found";
    else
       cout<<"Element found at index"<<index;
        return 0; 
} 

Please find a photo of the code and output for your reference:


Related Solutions

Consider the x, y data: x-data (explanatory variables): 10, 15, 20, 25, 30, 35, 40, 45,...
Consider the x, y data: x-data (explanatory variables): 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 y-data (response variables): 1359.9265, 1353.3046, 220.7435, 964.6208, 1861.9920, 1195.3707, 1702.0145, 2002.0900, 1129.1860, 1864.5241, 1444.2239, 2342.5453, 2410.9056, 2766.2245, 2135.2241, 3113.7662, 4311.7260, 3313.1042, 4072.0945 Compute a best fit line to the data. Report: a. The slope coefficient, β1:   b. The intercept coefficient, β0:    c. The standard error of the residuals σε:   d. The Adjusted...
Consider the x, y data: x-data (explanatory variables): 10, 15, 20, 25, 30, 35, 40, 45,...
Consider the x, y data: x-data (explanatory variables): 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100 y-data (response variables): 1359.9265, 1353.3046, 220.7435, 964.6208, 1861.9920, 1195.3707, 1702.0145, 2002.0900, 1129.1860, 1864.5241, 1444.2239, 2342.5453, 2410.9056, 2766.2245, 2135.2241, 3113.7662, 4311.7260, 3313.1042, 4072.0945 Compute a best fit line to the data. Report: a. The slope coefficient, β1: ___ b. The intercept coefficient, β0: ___ c. The standard error of the residuals σε: ___ d....
Q4: . The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20,...
Q4: . The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60. How many recursive calls are made by our binarySearch method given an initial invocation of binarySearch(45, 0, 15)? Answer Choices : 2     0     3     4     1
Q1: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24,...
Q1: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60. How many recursive calls are made by our binarySearch method, given an initial invocation of binarySearch(25, 0, 15)? Answer Choices : 3     2     4     0     1 Q2: The sorted values array contains the sixteen integers 1, 2, 3, 13, 13, 20, 24, 25, 30, 32, 40, 45, 50, 52, 57, 60....
Consider the following sorted int array: 1 3 4 5 7 9 10 12 If we...
Consider the following sorted int array: 1 3 4 5 7 9 10 12 If we search for the key value 10 in the array using the binary search algorithm, what is the sequence of indices that will be accessed in the array? (Hint: For a sublist between index values low and high, the middle index is calculated using: (low + high) / 2. So you need to show the sequence of indices of the middle index in each pass.)...
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted...
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Mergesort. Count the number of comparisons made. (b) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Heapsort.
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted...
(a) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Mergesort. Count the number of comparisonsmade. (b) Illustrate how the list 5, 13, 2, 25, 7, 17, 20, 8, 4 is sorted with Heapsort
Consider the data. xi 3 12 6 20 14 yi 65 40 45 15 25 The...
Consider the data. xi 3 12 6 20 14 yi 65 40 45 15 25 The estimated regression equation for these data is ŷ = 68.25 − 2.75x. (a) Compute SSE, SST, and SSR using equations SSE = Σ(yi − ŷi)2, SST = Σ(yi − y)2, and SSR = Σ(ŷi − y)2. SSE=SST=SSR= (b) Compute the coefficient of determination r2. (Round your answer to three decimal places.) r2 = Comment on the goodness of fit. (For purposes of this exercise,...
Sample5(ACTRESSES) Sample5(ACTORS) 38 37 45 43 25 42 28 43 40 44 22 41 35 56...
Sample5(ACTRESSES) Sample5(ACTORS) 38 37 45 43 25 42 28 43 40 44 22 41 35 56 34 44 24 47 33 29 33 31 26 37 32 34 21 32 29 51 33 42 26 60 41 32 54 52 43 39 54 54 35 45 35 34 27 47 35 60 29 37 29 53 35 60 31 33 30 38 The sample you were given in class comes from Data Set 14 “Oscar Winner Age” in your textbook....
Consider the set of integers A = {1, 2, 3, 4, 5}. Pairs of numbers are...
Consider the set of integers A = {1, 2, 3, 4, 5}. Pairs of numbers are constructed where each number of the pair comes from set A. Construct the sampling distribution of sample ranges. Apply the Empirical Rule to this distribution.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT