0% found this document useful (0 votes)
6 views5 pages

BS Using C

The document outlines an experiment to develop a Binary Search program using the Divide and Conquer approach, including objectives, required software, algorithm steps, and a sample C code implementation. It details the procedure for measuring the running time of the algorithm, along with observations, results, and discussions on its efficiency compared to linear search. Additionally, it includes precautions and viva questions related to the experiment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

BS Using C

The document outlines an experiment to develop a Binary Search program using the Divide and Conquer approach, including objectives, required software, algorithm steps, and a sample C code implementation. It details the procedure for measuring the running time of the algorithm, along with observations, results, and discussions on its efficiency compared to linear search. Additionally, it includes precautions and viva questions related to the experiment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Institution Name: [Enter Institution Name]

Department: [Enter Department Name]


Lab Name: [Enter Lab Name]
Course Title: [Enter Course Title]
Semester: [Enter Semester]
Batch: [Enter Batch Details]
Student Name: [Enter Student Name]
Roll Number: [Enter Roll Number]
Date of Experiment: [Enter Date]
Experiment No.: [Enter Experiment Number]
Experiment Title: Develop a Program and Measure the Running Time for Binary Search
Using Divide and Conquer

1. Objective

 To develop a program for Binary Search using the Divide and Conquer approach.
 To measure and analyze the running time of the Binary Search algorithm.

2. Software Required

 System with Python/C/C++/Java IDE installed.


 Text editor (like VS Code, Notepad++, etc.).
 Stopwatch or built-in time measurement functions (like time module in Python,
clock() in C/C++).

3. Algorithm

1. Start with the first index (low) and last index (high) of the array.
2. Calculate the middle index as: mid = (low + high) // 2.
3. If the middle element matches the target, the search is successful.
4. If the target is smaller than the middle element, search in the left sub-array.
5. If the target is larger than the middle element, search in the right sub-array.
6. Repeat steps 2-5 until the element is found or the sub-array reduces to zero.

4. Flowchart / Diagram

 Draw the flowchart for binary search with decision steps and loops.
 Include conditions like 'target == mid', 'target < mid', and 'target > mid'.
5. Theory

 Binary Search is a searching technique that divides the search space into two halves in
every iteration, reducing the problem size significantly.
 The time complexity of binary search is O(log n), making it much faster than linear
search for large datasets.
 The running time of binary search depends on the size of the input array.

6. Procedure

1. Initialize the sorted array, target value, and low and high indices.
2. Calculate the middle index.
3. Compare the middle element with the target.
4. If the middle element matches the target, return the index.
5. If the target is smaller, move to the left sub-array (update high = mid - 1).
6. If the target is larger, move to the right sub-array (update low = mid + 1).
7. Repeat the process until the target is found or the sub-array size becomes zero.
8. Measure the running time of the program using built-in time functions (like
time.time() in Python).

7. Program

Example C Code: Here’s a C program to implement Binary Search using Divide and
Conquer and measure its running time using the clock() function from the time.h library.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Function to perform binary search using divide and conquer approach


int binarySearch(int arr[], int low, int high, int key) {
if (low <= high) {
int mid = low + (high - low) / 2;

// Check if the key is present at mid


if (arr[mid] == key)
return mid;

// If key is smaller than mid, search the left sub-array


if (key < arr[mid])
return binarySearch(arr, low, mid - 1, key);

// Else search the right sub-array


return binarySearch(arr, mid + 1, high, key);
}
return -1; // Key not found
}

int main() {
int n, key, result;
clock_t start, end;
double cpu_time_used;

// Input the size of the array


printf("Enter the size of the array: ");
scanf("%d", &n);

// Allocate memory for the array


int *arr = (int *)malloc(n * sizeof(int));

// Input sorted array elements


printf("Enter %d sorted elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

// Input the key to be searched


printf("Enter the key to search: ");
scanf("%d", &key);

// Start time measurement


start = clock();

// Call the binary search function


result = binarySearch(arr, 0, n - 1, key);

// End time measurement


end = clock();

// Calculate time taken in seconds


cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;

// Display the result


if (result != -1)
printf("Key %d found at index %d.\n", key, result);
else
printf("Key %d not found in the array.\n", key);

printf("Time taken for Binary Search: %f seconds.\n", cpu_time_used);

// Free allocated memory


free(arr);
return 0;
}

8. Observations / Data Collection

Example of Data Table:

Array Size (n) Target Element Index Found Time Taken (seconds)
5 10 3 0.00002
10 40 9 0.00004
100 78 76 0.00012

9. Calculations (If applicable)

 Calculation of mid: mid = (low + high) // 2.


 Time complexity of binary search is O(log n).

10. Graphs / Plots (If applicable)

 Plot the graph of Array Size (n) vs. Time Taken (seconds) to visualize the time
complexity.
 The plot should show a logarithmic curve.

11. Result / Output

 The index of the target element in the sorted array is displayed.


 The time taken for the execution of the binary search is also displayed.
 Example output:
o Element found at index 3.
o Time taken for execution: 0.00002 seconds.

12. Inference / Discussion

 Binary search is faster than linear search due to its logarithmic time complexity.
 The running time increases as the size of the array increases but follows a logarithmic
growth.
 Real-time implementation of binary search can be optimized by using iterative
approaches instead of recursion.
13. Precautions

 Ensure the array is sorted before starting the search.


 Handle the case where the element is not found.
 Avoid infinite loops by ensuring proper updates to low and high indices.

14. Viva Questions

1. What is the time complexity of binary search?


2. How do you measure the running time of a program?
3. How does binary search differ from linear search?
4. What is the role of divide and conquer in binary search?
5. How would you modify the program to handle floating-point numbers?

15. Signature

 Student Signature: [Signature of Student]


 Lab Instructor Signature: [Signature of Lab Instructor]
 Date of Completion: [Date of Completion]

You might also like