Binary Search for Rational Numbers without using floating point arithmetic in C program

In this problem, we are given a sorted array of rational numbers and we have to search the given element using binary search algorithm without using floating point arithmetic.

A Rational number is a number represented in the form p/q where both p and q are integers. For example, 2/3, 1/5.

Binary search is a searching technique that works by finding the middle of the array and comparing elements to determine which half contains the target.

For finding the element using binary search from a sorted array of rational numbers, where floating point arithmetic is not allowed, we will compare the cross products of numerators and denominators to determine which element is greater.

Comparison Logic

To compare two rational numbers a/b and c/d without floating point arithmetic, we use cross multiplication −

  • If a × d = b × c, then the numbers are equal
  • If a × d > b × c, then a/b > c/d
  • If a × d , then a/b

Example

Let's create a program that implements binary search for rational numbers −

#include <stdio.h>

struct Rational {
    int p;  /* numerator */
    int q;  /* denominator */
};

int compare(struct Rational a, struct Rational b) {
    /* Compare a.p/a.q with b.p/b.q using cross multiplication */
    if (a.p * b.q == a.q * b.p)
        return 0;   /* Equal */
    if (a.p * b.q > a.q * b.p)
        return 1;   /* a > b */
    return -1;      /* a < b */
}

int binarySearch(struct Rational arr[], int l, int r, struct Rational x) {
    if (r >= l) {
        int mid = l + (r - l)/2;
        
        if (compare(arr[mid], x) == 0) 
            return mid;
        
        if (compare(arr[mid], x) > 0)
            return binarySearch(arr, l, mid-1, x);
        
        return binarySearch(arr, mid+1, r, x);
    }
    return -1;  /* Element not found */
}

int main() {
    struct Rational arr[] = {{1, 4}, {2, 3}, {3, 2}, {7, 2}};
    struct Rational x = {3, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    printf("Searching for %d/%d<br>", x.p, x.q);
    int result = binarySearch(arr, 0, n-1, x);
    
    if (result != -1)
        printf("Element found at index %d<br>", result);
    else
        printf("Element not found<br>");
    
    return 0;
}
Searching for 3/2
Element found at index 2

How It Works

  1. The compare() function uses cross multiplication to compare rational numbers without converting to decimal
  2. Binary search recursively divides the array in half based on comparison results
  3. The algorithm maintains O(log n) time complexity even with rational number comparisons

Conclusion

Binary search on rational numbers can be efficiently implemented using cross multiplication to avoid floating point arithmetic. This approach maintains the logarithmic time complexity while ensuring precise comparisons.

Updated on: 2026-03-15T12:27:12+05:30

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements