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

Binary Search C

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

Binary Search C

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

#include <stdio.

h>

// Iterative Binary Search implementation


int binary_search_iterative(int arr[], int size, int target) {
int left = 0;
int right = size - 1;

// Continue searching while the search range is valid


while (left <= right) {
// Calculate the middle index
int mid = left + (right - left) / 2;

// Check if target is present at mid


if (arr[mid] == target) {
return mid; // Target found, return its index
}

// If target is greater, ignore left half


if (arr[mid] < target) {
left = mid + 1;
}
// If target is smaller, ignore right half
else {
right = mid - 1;
}
}

// Target was not found


return -1;
}

// Recursive Binary Search implementation


int binary_search_recursive(int arr[], int left, int right, int target) {
// Base case: target not found
if (left > right) {
return -1;
}

// Calculate the middle index


int mid = left + (right - left) / 2;

// If target is present at mid, return its index


if (arr[mid] == target) {
return mid;
}

// If target is smaller than mid element, search left half


if (target < arr[mid]) {
return binary_search_recursive(arr, left, mid - 1, target);
}

// If target is larger than mid element, search right half


return binary_search_recursive(arr, mid + 1, right, target);
}

// Example usage of binary search


int main() {
// Sample SORTED array (binary search requires a sorted array)
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int size = sizeof(arr) / sizeof(arr[0]);

// Elements to search
int target1 = 50; // Element present in the array
int target2 = 35; // Element not in the array

// Iterative Binary Search


printf("Iterative Binary Search:\n");
int iterative_result1 = binary_search_iterative(arr, size, target1);
int iterative_result2 = binary_search_iterative(arr, size, target2);

if (iterative_result1 != -1) {
printf("%d found at index %d (Iterative)\n", target1, iterative_result1);
} else {
printf("%d not found in the array (Iterative)\n", target1);
}

if (iterative_result2 != -1) {
printf("%d found at index %d (Iterative)\n", target2, iterative_result2);
} else {
printf("%d not found in the array (Iterative)\n", target2);
}

// Recursive Binary Search


printf("\nRecursive Binary Search:\n");
int recursive_result1 = binary_search_recursive(arr, 0, size - 1, target1);
int recursive_result2 = binary_search_recursive(arr, 0, size - 1, target2);

if (recursive_result1 != -1) {
printf("%d found at index %d (Recursive)\n", target1, recursive_result1);
} else {
printf("%d not found in the array (Recursive)\n", target1);
}

if (recursive_result2 != -1) {
printf("%d found at index %d (Recursive)\n", target2, recursive_result2);
} else {
printf("%d not found in the array (Recursive)\n", target2);
}

return 0;
}

/*
Binary Search Key Characteristics:
1. PREREQUISITES:
- Array MUST be sorted in ascending order
- Elements must be comparable and have a natural ordering

2. Time Complexity:
- Best Case: O(1) - when middle element is the target
- Worst Case: O(log n) - when target is at the beginning/end or not present
- Average Case: O(log n)

3. Space Complexity:
- Iterative Version: O(1) - uses constant extra space
- Recursive Version: O(log n) - due to recursive call stack

4. Advantages:
- Extremely efficient for large sorted arrays
- Dramatically reduces search space in each iteration
- Much faster than linear search for large datasets

5. Disadvantages:
- Requires sorted array (preprocessing cost)
- Not suitable for unsorted arrays
- Less efficient for small arrays due to overhead
- More complex implementation compared to linear search

Comparison Notes:
- Linear Search: O(n) time complexity
- Binary Search: O(log n) time complexity
- Binary search becomes significantly more efficient as array size increases
*/

You might also like