Experiment Binary Search
Experiment Binary Search
List of Experiments
12. Write a Program to implement All Pairs Shortest Path Using Dynamic Programming.
2
Experiment 2
Objective: Write a program to implement Binary Search using Divide and Conquer.
Theory: Binary search is defined as a searching algorithm used in a sorted array by repeatedly
dividing the search interval in half. The idea of binary search is to use the information that the
array is sorted and reduce the time complexity to O(log N).
Conditions to implement Binary Search:
To apply Binary Search algorithm:
1. The data structure must be sorted.
2. Access to any element of the data structure takes constant time.
Binary Search Algorithm:
There are following steps involved in the binary search algorithm:
1. Divide the search space into two halves by finding the middle index “mid”
Finding the middle index “mid” in Binary Search Algorithm
mid = (low + high)/2
2. Compare the middle element of the search space with the key.
3. if the key is not found at middle element, choose which half will be used as the next
search space.
a. if the key is smaller than the middle element, then the right side is used for the
next search.
b. if the key is larger than the middle element, the right side is used for the next
search.
4. This process is continued unit the key is found or the total search space is exhausted.
How does Binary Search Work?
To understand the working of binary, consider the following illustration:
Consider an array arr[]= {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target= 23.
First step: Calculate the mid and compare the mid element with the key. if the key is less
than mid element, move to left and if it is greater than the mid then move search space to
the right.
Key (i.e. 23) is greater than current mid element (i.e. 16). The search space
moves to the right.
o Binary Search Algorithm: Compare key with 16
Key is less than the current mid-56. The search space moves to the left.
3
Second Step: If the key matches the value of the mid element, the element is found and stop
search.
4
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// If we reach here, then element was not present
return -1;
}
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int x = 10;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
(result == -1)
? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
return 0;
}
5
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// If we reach here, then element was not present
return -1;
}
// Driver code
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not present"
" in array")
: printf("Element is present at "
"index %d",
result);
return 0;
}
// Driver code
public static void main(String args[])
{
BinarySearch ob = new BinarySearch();
int arr[] = { 2, 3, 4, 10, 40 };
7
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println(
"Element is not present in array");
else
System.out.println("Element is present at "
+ "index " + result);
}
}
while l <= r:
mid = l + (r - l) // 2
# Check if x is present at mid
if arr[mid] == x:
return mid
# If x is greater, ignore left half
elif arr[mid] < x:
l = mid + 1
# If x is smaller, ignore right half
else:
r = mid - 1
# If we reach here, then the element
# was not present
return -1
8
# Driver Code
if __name__ == '__main__':
arr = [2, 3, 4, 10, 40]
x = 10
# Function call
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index", result)
else:
print("Element is not present in array")
Experiment 2
Objective:
Experiment 3
Objective:
Experiment 4
Objective:
Experiment 5
Objective:
Experiment 6
Objective:
9
Experiment 7
Objective:
Experiment 8
Objective:
Experiment 9
Objective:
Experiment 10
Objective:
Experiment 11
Objective:
Experiment 12
Objective:
10