C Program to Perform Uniform Binary Search

This C program performs uniform binary search.

Uniform binary search is an optimization of the classic binary search algorithm invented by Donald Knuth and given in Knuth’s The Art of Computer Programming. It uses a lookup table to update a single array index, rather than taking the midpoint of an upper and a lower bound on each iteration; therefore, it is optimized for architectures (such as Knuth’s MIX) on which

  • a table lookup is generally faster than an addition and a shift, and
  • many searches will be performed on the same array, or on several arrays of the same length

Here is the source code of the C program to uniform binary search on integer array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #define LOG_N 42
  2.  
  3. static int delta[LOG_N];
  4.  
  5. void make_delta(int N)
  6. {
  7.     int power = 1;
  8.     int i = 0;
  9.     do {
  10.         int half = power;
  11.         power <<= 1;
  12.         delta[i] = (N + half) / power;
  13.     } while (delta[i++] != 0);
  14. }
  15.  
  16. int unisearch(int *a, int key)
  17. {
  18.     int i = delta[0]-1;  /* midpoint of array */
  19.     int d = 0;
  20.  
  21.     while (1) {
  22.         if (key == a[i]) {
  23.             return i;
  24.         } else if (delta[d] == 0) {
  25.             return -1;
  26.         } else {
  27.             if (key < a[i]) {
  28.                 i -= delta[++d];
  29.             } else {
  30.                 i += delta[++d];
  31.             }
  32.         }
  33.     }
  34. }
  35.  
  36. #define N 10
  37. int main(void)
  38. {
  39.     int num, a[N] = {1,3,5,6,7,9,14,15,17,19};
  40.     make_delta(N);
  41.     printf("\nEnter an element to search: ");
  42.     scanf("%d", &num);
  43.     printf("%d is at index %d\n", num, unisearch(a, num));
  44.     return 0;
  45. }

$ gcc uniformbinarysearch.c -o uniformbinarysearch
$ ./uniformbinarysearch
 
Enter an element to search: 19
19 is at index 9

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.