answersLogoWhite

0


Best Answer

The array has eight elements indexed 0 to 7. The middle element will be found at index (0+7)/2=3. Element 3 has the value 26, which is smaller than 88, so 88 must reside in the array starting at index 4 ending at index 7.

The array has four elements indexed 4 to 7. The middle element can be found at index (4+7)/2=5. Element 5 has the value 56, which is smaller than 88, so 88 must reside in the array starting at index 6 ending at index 7.

The array has two elements indexed 6 to 7. The middle element can be found at index (6+7)/2=6. Element 6 has the value 88. Thus 88 was found at index 6.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

Function definition:


size_t binary_search (const vector& v, const unsigned value)

{

size_t low=0;

size_t high=v.size();

while (low

{

size_t mid = low + ((high-low) / 2);

if (v[mid]==value)

return mid;

if (v[mid]

low = mid + 1;

else

high = mid;

}

return v.size();

}


Test function:


int main()

{

vector A {8, 13, 17, 26, 44, 56, 88, 97};

size_t index = binary_search (A, 88);

assert (index==6);

}


1st iteration of while loop:


low = 0;

high = 8;

mid = 4; // 0 + ((8 - 0) / 2) = 0 + (8 / 2) = 0 + 4 = 4

A[4] is 44 which is less than 88, so low becomes 4 + 1 = 5.


2nd iteration of while loop:


low = 5;

high = 8;

mid = 6; // 5 + ((8 - 5) / 2) = 5 + (3 / 2) = 5 + 1 = 6

A[6] is 88 which is equal to 88 so 6 is returned.



If we search for a value that does not exist, such as 42, the following would occur instead:


1st iteration of while loop:


low = 0;

high = 8;

mid = 4; // 0 + ((8 - 0) / 2) = 0 + (8 / 2) = 0 + 4 = 4

A[4] is 44 which is greater than 42, so high becomes 4.


2nd iteration of while loop:


low = 0;

high = 4;

mid = 2; // 0 + ((4 - 0) / 2) = 0 + (4 / 2) = 0 + 2 = 2

A[2] is 17 which is less than 42, so low becomes 2 + 1 = 3.


3rd iteration of while loop:


low = 3;

high = 4;

mid = 2; // 3 + ((4 - 3) / 2) = 3 + (1 / 2) = 3 + 0 = 3

A[3] is 26 which is less than 42, so low becomes 3 + 1 = 4.


At this point, low is equal to high, so the loop terminates and we return the size of the vector (8). Since there is no element with index 8 (the valid range being A[0] to A[7]), this tells us the value does not exist. We can verify this with the following test function.


int main()

{

vector A {8, 13, 17, 26, 44, 56, 88, 97};

size_t index = binary_search (A, 42);

assert (index==A.size());

}


This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Given a sorted array- 8 13 17 26 44 56 88 97. Using binary search method find the element 88. show the contents of high low and mid at each step?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Which search algorithm requires that the arrays contents be sorted?

Binary Search Algorithm


What is the advantage of the binary search method?

It gets you to the answer with fewer steps.


When is it not recommended to perform the operation of binary search?

In computer science, a binary search or half-interval search algorithm finds the position of a specified value (the input "key") in an array sorted[1][2] into order on the values of the key. At each stage, the algorithm compares the sought key value with the key value of the middle element of the array. If the keys match, then a matching element has been found so its index is returned. Otherwise, if the sought key is less than the middle element's key, then the algorithm repeats its action on the subarray to the left of the middle element or, if the input key is greater, on the subarray to the right. If the array span to be searched is reduced to zero, then the key cannot be found in the array and a special "Not found" indication is returned.A binary search halves the number of items to check with each iteration, so locating the an item (or determining its absence) takes logarithmic time. A binary search is a dichotomic divide and conquer search algorithm.Next AnswerA binary search method requires that the list of items being search should be sorted in ascending (or descending) order. If the search list is not sorted, the binary search method will not work and other search methods will be needed.


How can one perform a binary search?

One can perform a binary search easily in many different ways. One can perform a binary search by using an algorithm specifically designed to test the input key value with the value of the middle element.


Does binary search tree take much time than the list to delete or insert an element?

No. Binary search tree will take less time to delete or insert an element. While deleting from list, more time will be required to search for the element to be deleted but BST will work faster if the no. of elements are more. Plus it depends upon which element is to be deleted and where the element is to be added. But the average time to perform these operations will be less in BST as compared to lists


How to search the contents of an array by accessing each element only once?

Sequentially, ie one-by-one.


Given an array of sorted list of integer numbers write function to search for particular item using binary search method?

//c++ sortdlist //in main method #include<iostream.h> void main() { Sortd s; int x; for (int i=0; i<10; i++) { s.insertItem(x) } // new enter 10 element type integer }


Which are the searching algorithm always compare the middle element with the searching elements in the given array?

binary search system


What is the recurrence relation for searching an array element using divide and conquer method?

The divide and conquer method of searching(also called binary search) can be applied only if the array is already sorted. This method divided the array into two halves and discards one half in every iteration. The time taken to compare whether the middle element is the required element is constant. Hence the recurrence relation can be represented as:T(n) = T(n/2) + O(1)= O(log n)


What assumption about the list is made when binary search is conducted?

Binary search requires that the list be in search key order.


What is the binary search tree worst case time complexity?

Binary search is a log n type of search, because the number of operations required to find an element is proportional to the log base 2 of the number of elements. This is because binary search is a successive halving operation, where each step cuts the number of choices in half. This is a log base 2 sequence.


What is the output of C programming of binary searching?

The output of a binary search routine is either (usually) the address of the element that matched the search term, or, if there was no match, the address of where the new element should be placed. Of course, this means that there are two outputs, one, an address, and two, whether or not the search term was found; which means that a single valued function will not suffice - it is necessary that one of the parameters be an address, perhaps of the flag variable.