Last active
May 28, 2022 02:53
-
-
Save Per48edjes/2528d3954b9c51ea5bd2a27211087a44 to your computer and use it in GitHub Desktop.
Implementation of binary search on sorted array of integers in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int binary_search(const int *arr, const int arr_len, const int n); | |
int main(int argc, char *argv[]) | |
{ | |
if (argc != 2) | |
{ | |
puts("Invalid argument specification."); | |
return !EXIT_SUCCESS; | |
} | |
const int sorted_items[] = { -1000, -21, 2222, 305840, 400000, 5123456, 6000000, 999999999 }; | |
const int len_items = sizeof(sorted_items) / sizeof(*sorted_items); | |
const int search_number = atoi(argv[1]); | |
// Binary search | |
int idx = binary_search(sorted_items, len_items, search_number); | |
if (idx >= 0) | |
{ | |
printf("Input number found at index %d in `sorted_items`!\n", idx); | |
return EXIT_SUCCESS; | |
} | |
else | |
{ | |
puts("Input number not found in `sorted_items`!"); | |
return !EXIT_SUCCESS; | |
} | |
} | |
/* Iterative implementation */ | |
int binary_search(const int *arr, const int arr_len, const int n) | |
{ | |
int start_idx = 0; | |
int end_idx = arr_len - 1; | |
int mid_idx; | |
// "Edge" cases | |
if ((arr_len == 0) || (n < arr[0]) || (n > arr[arr_len - 1])) | |
{ | |
return -1; | |
} | |
do | |
{ | |
mid_idx = start_idx + ((end_idx - start_idx) / 2); | |
if (arr[mid_idx] > n) | |
{ | |
end_idx = mid_idx; | |
} | |
else if (arr[mid_idx] < n) | |
{ | |
start_idx = mid_idx; | |
} | |
else | |
{ | |
return mid_idx; | |
} | |
} while ((end_idx - start_idx) > 1); | |
return -1; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
import sys | |
def main(n: int) -> None: | |
""" | |
Program entrypoint to search pre-defined `sorted_arr` for `n` from user input | |
""" | |
rand_arr = [random.randint(-20, 20) for _ in range(10)] | |
sorted_arr = sorted(rand_arr) | |
print(sorted_arr) | |
if len(sorted_arr) == 0: | |
print(f"The number {n} was NOT found because `sorted_arr` is empty.") | |
else: | |
found_idx = binary_search(sorted_arr, n) | |
if found_idx > 0: | |
print(f"The number {n} was found at index {found_idx}!") | |
else: | |
print(f"The number {n} was NOT found.") | |
return | |
def binary_search( | |
sorted_arr: list, n: int, start_idx: int = 0, end_idx: int = None | |
) -> int: | |
""" | |
Recursive implementation of binary search for `n` over `sorted_arr` | |
""" | |
end_idx = len(sorted_arr) - 1 if end_idx is None else end_idx | |
mid_idx = start_idx + ((end_idx - start_idx) // 2) | |
# Base case | |
if len(sorted_arr[start_idx:end_idx]) == 1: | |
return start_idx if sorted_arr[0] == n else -1 | |
# Recursive case | |
if sorted_arr[mid_idx] == n: | |
return mid_idx | |
elif sorted_arr[mid_idx] > n: | |
return binary_search(sorted_arr, n, start_idx, mid_idx) | |
else: | |
return binary_search(sorted_arr, n, mid_idx, end_idx) | |
if __name__ == "__main__": | |
n = int(sys.argv[1]) | |
main(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added recursive implementation of binary search in Python as supplement to the iterative implementation in C.