From 7239949e1e00106b866f2f95300f51932e794dc9 Mon Sep 17 00:00:00 2001 From: ADIL AHMED Date: Sun, 23 Aug 2026 17:51:28 +0530 Subject: [PATCH 1/3] Update binary_search.c This version of code has 2 different approach (iterative and recursive ) and taking that the given array is not sorted most optimized solution for sorting is involved as well as their respective time complexity is mentioned suggesting which approach is better (iterative) . And the test case and edge cases are also successfully handled. --- C/algorithms/searching/binary_search.c | 160 +++++++++++++++++++------ 1 file changed, 124 insertions(+), 36 deletions(-) diff --git a/C/algorithms/searching/binary_search.c b/C/algorithms/searching/binary_search.c index 0c69ed23..171e426a 100644 --- a/C/algorithms/searching/binary_search.c +++ b/C/algorithms/searching/binary_search.c @@ -1,42 +1,130 @@ /* -* Algorithm: [Binary Search] - * Description: [Binary search is an efficient algorithm used to find the position of a target value within a sorted array or list. It works on the principle of repeatedly dividing the search interval in half.] - * Time Complexity : - * Best Case : O(1) // when given array is already sorted. - * Average Case : O(log n) //when given array is in random order - * Worst Case : O(log n) // when given array is in reverse order + * Algorithm: Binary Search + * Description: Binary Search basically searches a target from a 1D array if exists,then + it returns the index number of the element if fails to find then it returns -1; + It actually breaks the array(sorted) into 2 halves and checks if the mid index + has the element,if it finds then returns ,else repeat the process, till the end; + + * Time Complexity: + Binary Search Iterative : O(log n) | (if array is already sorted); + : O(log n + n^2) | (if have to sort the array); + Binary Search Recursive : O(log n) | (if array is already sorted); + : O(log n + n^2) |(if have to sort the array); + * Space Complexity: - * Worst : 0(1) - * Author: [Kaustubh Udavant] + Binary Search Iterative : O(1) (constant space) + Binary Search Recursive : O(log n) (stack frame space) + + * Author: ADIL AHMED */ - + +#include #include -#define SIZE 10 -int BinarySearch(int [],int); -int main() -{ - int a[SIZE]={3,5,9,11,15,17,22,25,37,68},key,pos; - printf("Enter the Search Key\n"); - scanf("%d",&key); - pos=BinarySearch(a,key); - if(pos==-1) - printf("The search key is not in the array\n"); - else - printf("The search key %d is at location %d\n",key,pos); - return 0; +#include + +/** + * Brief description of the functions : + * There are total 5 functions in the code excluding the main() function. + * + * Binary_search_iterative : perform binary search in iterative way. + * Binary_search_recursive : perform binary search with help of recursion; + * compare_ints : Compares two numbers to determine ascending or descending sorting order. + * insertion_sort : Sorts small arrays in-place with excellent CPU cache efficiency. + * universal_sort : Switches algorithms automatically based on array size for speed. + * + * + * @param arr: int arr[]={3,4,1,67} //taken as a test case + * @param size: Size of the array is 4; //n=4 + * @return: The return value of the Binary_search_iterative & Binary_search_recursive both is + * the index value from the given array of the target (if exists) else -1; + */ + +int Binary_search_iterative(int arr[][2],int n,int target){ + int low=0,high=n-1; + while(low<=high){ + int mid=low+(high-low)/2; + if(arr[mid][0]==target) return arr[mid][1]; + else if(arr[mid][0]>target) high=mid-1; + else low=mid+1; } + return -1; +} + +int Binary_search_recursive(int arr[][2],int target,int low,int high){ + if(low<=high){ + int mid=low+(high-low)/2; + if(arr[mid][0]==target) return arr[mid][1]; + else if(arr[mid][0]>target) return Binary_search_recursive(arr,target,low,mid-1); + else return Binary_search_recursive(arr,target,mid+1,high); + } + return -1; +} + + +// Standard comparison function +int compare_ints(const void *a, const void *b) { + int val_a = ((const int *)a)[0]; + int val_b = ((const int *)b)[0]; + return (val_a > val_b) - (val_a < val_b); +} + +// Inlined Insertion Sort for small workloads +static inline void insertion_sort(int arr[][2], int n) { + for (int i = 1; i < n; i++) { + int key_val = arr[i][0]; + int key_idx = arr[i][1]; + int j = i - 1; + while (j >= 0 && arr[j][0] > key_val) { + arr[j + 1][0] = arr[j][0]; + arr[j + 1][1] = arr[j][1]; + j--; + } + arr[j + 1][0] = key_val; + arr[j + 1][1] = key_idx; + } +} + +// Universal optimized entry point +void universal_sort(int arr[], int n) { + if (n <= 1) return; // having 1 element means already sorted + if (n <= 16) {// for less than 16 sized arrays insertion_sort is highly optimised + insertion_sort(arr, n); + } else {// for other cases the builtin qsort() function of c is enough optimised + qsort(arr, n, sizeof(int)*2, compare_ints); + } +} + + +/** + * Integrated test function demonstrating the binary search algorithm + */ +void test_algorithm() { + printf("Testing Algorithm...\n\n"); + + // Test Case 1: Normal case + int raw_arr1[] = {3, 4, 1, 67}; + int size1 = sizeof(raw_arr1) / sizeof(raw_arr1[0]); + int target1 = 4; + + int test_arr1[size1][2]; + for(int i = 0; i < size1; i++) { + test_arr1[i][0] = raw_arr1[i]; + test_arr1[i][1] = i; + } + + universal_sort(test_arr1, size1); + int result1 = Binary_search_iterative(test_arr1, size1, target1); + printf("Test 1 - Input: {3,4,1,67}, Target: %d, Original Index: %d\n successfully handled \n\n", target1, result1); + + // Test Case 2: Edge case (Empty/Null check) + int (*test_arr2)[2] = NULL; + int result2 = Binary_search_iterative(test_arr2, 0, 4); + printf("Test 2 - Edge case handled: %d\n successfully handled \n\n", result2); +} + +int main() { + // Run the integrated test pipeline + test_algorithm(); + return 0; +} - int BinarySearch (int A[],int skey) - { - int low=0,high=SIZE-1,middle; - while (low <=high){ - middle=(low+high)/2; - if(skey==A[middle]) - return middle; - else if(skey Date: Sun, 23 Aug 2026 17:54:26 +0530 Subject: [PATCH 2/3] Update binary_search.c --- C/algorithms/searching/binary_search.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/C/algorithms/searching/binary_search.c b/C/algorithms/searching/binary_search.c index 171e426a..33cc5e22 100644 --- a/C/algorithms/searching/binary_search.c +++ b/C/algorithms/searching/binary_search.c @@ -24,7 +24,7 @@ /** * Brief description of the functions : - * There are total 5 functions in the code excluding the main() function. + * There are total 5 functions in the code excluding the main() & test_algorithm() function. * * Binary_search_iterative : perform binary search in iterative way. * Binary_search_recursive : perform binary search with help of recursion; From 152392c86302d93f00be3aa82f856075a77da9b9 Mon Sep 17 00:00:00 2001 From: ADIL AHMED Date: Sun, 23 Aug 2026 18:52:16 +0530 Subject: [PATCH 3/3] Update binary_search.c updated the code according the requirements and deleted unwanted blocks --- C/algorithms/searching/binary_search.c | 150 +++++++++---------------- 1 file changed, 51 insertions(+), 99 deletions(-) diff --git a/C/algorithms/searching/binary_search.c b/C/algorithms/searching/binary_search.c index 33cc5e22..30379ea6 100644 --- a/C/algorithms/searching/binary_search.c +++ b/C/algorithms/searching/binary_search.c @@ -1,130 +1,82 @@ -/* - * Algorithm: Binary Search - * Description: Binary Search basically searches a target from a 1D array if exists,then - it returns the index number of the element if fails to find then it returns -1; - It actually breaks the array(sorted) into 2 halves and checks if the mid index - has the element,if it finds then returns ,else repeat the process, till the end; - - * Time Complexity: - Binary Search Iterative : O(log n) | (if array is already sorted); - : O(log n + n^2) | (if have to sort the array); - Binary Search Recursive : O(log n) | (if array is already sorted); - : O(log n + n^2) |(if have to sort the array); - - * Space Complexity: - Binary Search Iterative : O(1) (constant space) - Binary Search Recursive : O(log n) (stack frame space) - + /* + * Algorithm: Binary Search (Iterative and Recursive) + * Description: An efficient algorithm used to find the position of a target value within a sorted array. + * Time Complexity: O(log n) + * Space Complexity: O(1) for Iterative, O(log n) for Recursive stack space * Author: ADIL AHMED */ - -#include + #include #include +#include /** - * Brief description of the functions : - * There are total 5 functions in the code excluding the main() & test_algorithm() function. - * - * Binary_search_iterative : perform binary search in iterative way. - * Binary_search_recursive : perform binary search with help of recursion; - * compare_ints : Compares two numbers to determine ascending or descending sorting order. - * insertion_sort : Sorts small arrays in-place with excellent CPU cache efficiency. - * universal_sort : Switches algorithms automatically based on array size for speed. - * + * Performs iterative binary search on a sorted array * - * @param arr: int arr[]={3,4,1,67} //taken as a test case - * @param size: Size of the array is 4; //n=4 - * @return: The return value of the Binary_search_iterative & Binary_search_recursive both is - * the index value from the given array of the target (if exists) else -1; + * @param arr: Sorted array to process + * @param size: Size of the array + * @param target: Element to search for + * @return: Index of target element if found, otherwise -1 */ - -int Binary_search_iterative(int arr[][2],int n,int target){ - int low=0,high=n-1; - while(low<=high){ - int mid=low+(high-low)/2; - if(arr[mid][0]==target) return arr[mid][1]; - else if(arr[mid][0]>target) high=mid-1; - else low=mid+1; +int Binary_search_iterative(int arr[], int size, int target) { + // Input validation + if (arr == NULL || size <= 0) { + return -1; } - return -1; -} -int Binary_search_recursive(int arr[][2],int target,int low,int high){ - if(low<=high){ - int mid=low+(high-low)/2; - if(arr[mid][0]==target) return arr[mid][1]; - else if(arr[mid][0]>target) return Binary_search_recursive(arr,target,low,mid-1); - else return Binary_search_recursive(arr,target,mid+1,high); + int low = 0, high = size - 1; + while (low <= high) { + int mid = low + (high - low) / 2; + if (arr[mid] == target) return mid; + else if (arr[mid] > target) high = mid - 1; + else low = mid + 1; } return -1; } - -// Standard comparison function -int compare_ints(const void *a, const void *b) { - int val_a = ((const int *)a)[0]; - int val_b = ((const int *)b)[0]; - return (val_a > val_b) - (val_a < val_b); -} - -// Inlined Insertion Sort for small workloads -static inline void insertion_sort(int arr[][2], int n) { - for (int i = 1; i < n; i++) { - int key_val = arr[i][0]; - int key_idx = arr[i][1]; - int j = i - 1; - while (j >= 0 && arr[j][0] > key_val) { - arr[j + 1][0] = arr[j][0]; - arr[j + 1][1] = arr[j][1]; - j--; - } - arr[j + 1][0] = key_val; - arr[j + 1][1] = key_idx; - } -} - -// Universal optimized entry point -void universal_sort(int arr[], int n) { - if (n <= 1) return; // having 1 element means already sorted - if (n <= 16) {// for less than 16 sized arrays insertion_sort is highly optimised - insertion_sort(arr, n); - } else {// for other cases the builtin qsort() function of c is enough optimised - qsort(arr, n, sizeof(int)*2, compare_ints); +/** + * Performs recursive binary search helper on a sorted array + * + * @param arr: Sorted array to process + * @param target: Element to search for + * @param low: Lower bound index + * @param high: Upper bound index + * @return: Index of target element if found, otherwise -1 + */ +int Binary_search_recursive(int arr[], int target, int low, int high) { + if (low <= high) { + int mid = low + (high - low) / 2; + if (arr[mid] == target) return mid; + else if (arr[mid] > target) return Binary_search_recursive(arr, target, low, mid - 1); + else return Binary_search_recursive(arr, target, mid + 1, high); } + return -1; } /** - * Integrated test function demonstrating the binary search algorithm + * Test function to demonstrate the algorithm */ void test_algorithm() { - printf("Testing Algorithm...\n\n"); + printf("Testing Algorithm...\n"); // Test Case 1: Normal case - int raw_arr1[] = {3, 4, 1, 67}; - int size1 = sizeof(raw_arr1) / sizeof(raw_arr1[0]); - int target1 = 4; - - int test_arr1[size1][2]; - for(int i = 0; i < size1; i++) { - test_arr1[i][0] = raw_arr1[i]; - test_arr1[i][1] = i; - } + int test_arr1[] = {1, 2, 3, 4, 5}; + int size1 = sizeof(test_arr1) / sizeof(test_arr1[0]); + int target1=4; + int result1 = Binary_search_iterative(test_arr1, size1,target1); + printf("Test 1 - Input: {1,2,3,4,5}, Result: %d\n", result1); - universal_sort(test_arr1, size1); - int result1 = Binary_search_iterative(test_arr1, size1, target1); - printf("Test 1 - Input: {3,4,1,67}, Target: %d, Original Index: %d\n successfully handled \n\n", target1, result1); + int result2 = Binary_search_recursive(test_arr1,target1,0,size1-1); + printf("Test 1 - Input: {1,2,3,4,5}, Result: %d\n", result2); - // Test Case 2: Edge case (Empty/Null check) - int (*test_arr2)[2] = NULL; - int result2 = Binary_search_iterative(test_arr2, 0, 4); - printf("Test 2 - Edge case handled: %d\n successfully handled \n\n", result2); + // Test Case 2: Edge case + int *test_arr2 = NULL; + int result3 = Binary_search_iterative(test_arr2, 0,target1); + printf("Test 2 - Edge case handled: %d\n", result3); } int main() { - // Run the integrated test pipeline test_algorithm(); return 0; } -