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....
Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
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
Write a program to compute intersection of two sorted array of integers and compute the CPU...
Write a program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator. Test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE NO HASH SET
you are asked to implement a C++ class to model a sorted array of unsigned integers....
you are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Class will provide (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be used...
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.)...
63, 40, 40, 39, 56, 45, 48, and 76 a. Find the sample mean b. Find...
63, 40, 40, 39, 56, 45, 48, and 76 a. Find the sample mean b. Find the sample median c. Find the sample mode d. Find the sample range e. Find the lower and upper quarrtiles, and the sample interquartile range IQR Q1= Q3= IQR= f. Display the data with a boxplot (with fences and whiskers) Fences: g. Write a brief description of this data (symmetry, outliers) h. Which is the better summary measure of these data, the mean or...
Consider an array A[1 · · · n] which is sorted and then rotated k steps...
Consider an array A[1 · · · n] which is sorted and then rotated k steps to the right. For example, we might start with the sorted array [1, 4, 5, 9, 10], and rotate it right by k = 3 steps to get [5, 9, 10, 1, 4]. Give an O(log n)-time algorithm that finds and returns the position of a given element x in array A, or returns None if x is not in A. Your algorithm is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT