0% found this document useful (0 votes)
6 views

Experiment Binary Search

Uploaded by

sarthakverma2503
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Experiment Binary Search

Uploaded by

sarthakverma2503
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

List of Experiments

1. Write a Program to implement Insertion sort.

2. Write a Program to implement Binary Search using Divide and Conquer.

3. Write a Program to implement Quicksort.

4. Write a Program to implement shortest path algorithm.

5. Write a Program to implement Merge sort using Divide and Conquer.

6. Write a Program to implement Knapsack problem using Greedy method.

7. Write a Program to implement Prim’s algorithm using Greedy method.

8. Write a Program to implement Kruskal’s algorithm using Greedy method.

9. Write a Program to implement Graph Traversal: Breadth First Traversal.

10. Write a Program to implement Graph Traversal: Depth First Traversal.

11. Write a Program to implement 8-Queen’s problem using Backtracking.

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.

How to implement binary search?


The Binary Search Algorithms (BSA) can be implemented in the following two ways
 Iterative Binary Search Algorithm
 Recursive Binary Search Algorithm
Given below are the pseudocodes for the approaches,
1. Iterative Binary Search Algorithm:
Here we use a while loop to continue the process of comparing the key and splitting the
search in two halves.

// C++ program to implement iterative Binary Search


#include <bits/stdc++.h>
using namespace std;
// An iterative binary search function.
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return m;

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;
}

// C program to implement iterative Binary Search


#include <stdio.h>
// An iterative binary search function.
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;

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;
}

// Java implementation of iterative Binary Search


6
import java.io.*;
class BinarySearch {
// Returns index of x if it is present in arr[].
int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
// 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
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);
}
}

# Python3 code to implement iterative Binary Search


# It returns location of x in given array arr
def binarySearch(arr, l, r, x):

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

You might also like