Given an array arr={12,34,47,62,85,92,95,99,105} &key=34; what are the mid values (corresponding array elements) generated in the first & second iterations
Answers
Answered by
6
85,92
.....................!!!
Answered by
1
BINARY SEARCH
Explanation:
Given an array
12,34,47,62,85,92,95,99,105
Key=34
So as per question we have to use binary search
In binary search algorithim:
Low=0;
High=len(array)
While(low<high)
{
Mid=low+high/2;
If array[mid] ==key
Print(key is found )
Else if(array[mid]<key)
Low=mid+1
Else
High=mid-1
}
So considering the array as sorted order
Step1)
12,34,47,62,85,92,95,99,105
0 1 2 3 4 5 6 7 8
Mid=(8+0)/2
Mid=4
Array[mid]=85
In step 1
Step2)
As 85>34
High=mid-1
So
12,34,47,62
0 1 2 3
Mid=3/2
Mid=1
Array[mid]=34
We got the key element now
In this process the mid elements produced are 85 and 34
Similar questions