ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Binary Search on Sorted Array

C

Goal -- WPM

Ready
Exercise Algorithm Area
1#include <stdio.h>\n\n// Function to perform binary search on a sorted array\n// Returns the index of the target if found, otherwise -1\nint binarySearch(int arr[], int low, int high, int target) {\n // Ensure the array is not empty and the range is valid\n if (high < low) {\n return -1; // Target not found\n }\n\n // Calculate mid point to avoid overflow\n int mid = low + (high - low) / 2;\n\n // If target is present at mid\n if (arr[mid] == target) {\n return mid;\n }\n\n // If target is smaller than mid, then it can only be present in left subarray\n if (arr[mid] > target) {\n return binarySearch(arr, low, mid - 1, target);\n }\n\n // Else the target can only be present in right subarray\n return binarySearch(arr, mid + 1, high, target);\n}\n\nint main() {\n int arr[] = {2, 3, 4, 10, 40};\n int n = sizeof(arr) / sizeof(arr[0]);\n int target = 10;\n\n // Handle empty array case explicitly\n if (n == 0) {\n printf("Array is empty.\n");\n return 1;\n }\n\n int result = binarySearch(arr, 0, n - 1, target);\n if (result == -1) {\n printf("Element %d is not present in array\n", target);\n } else {\n printf("Element %d is present at index %d\n", target, result);\n }\n\n target = 5; // Test for a non-existent element\n result = binarySearch(arr, 0, n - 1, target);\n if (result == -1) {\n printf("Element %d is not present in array\n", target);\n } else {\n printf("Element %d is present at index %d\n", target, result);\n }\n\n return 0;\n}\n
Algorithm description viewbox

Binary Search on Sorted Array

Algorithm description:

This C code implements a recursive binary search algorithm. It efficiently finds the index of a target value within a sorted array by repeatedly dividing the search interval in half. Binary search is commonly used in applications requiring fast lookups, such as searching in databases or dictionaries.

Algorithm explanation:

Binary search works on the principle of divide and conquer. Given a sorted array and a target value, it first checks the middle element. If the middle element is the target, the search is complete. If the target is less than the middle element, the search continues in the left half of the array. If the target is greater, the search continues in the right half. This process is repeated until the target is found or the search interval becomes empty. The time complexity is O(log n) because the search space is halved in each step. The space complexity is O(log n) for the recursive version due to the call stack, or O(1) for an iterative version. Edge cases include an empty array, a target not present, and the target being the first or last element.

Pseudocode:

function binarySearch(arr, low, high, target):
  if high < low:
    return -1
  mid = low + (high - low) / 2
  if arr[mid] == target:
    return mid
  else if arr[mid] > target:
    return binarySearch(arr, low, mid - 1, target)
  else:
    return binarySearch(arr, mid + 1, high, target)