• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Implement a generic binary search algorithm for Integer Double String etc

March 20, 2014 by Dhaval Dave

A Simple Binary Search for Integers

BinarySearch(A[0..N-1], value) 
{
   low = 0
   high = N - 1
   while (low <= high) {
       mid = low + ((high - low) / 2)
       if (A[mid] > value)
           high = mid - 1
       else if (A[mid] < value)
           low = mid + 1
       else
           return mid // found
    }//while end
   return -1 // not found
}


To implement  Generic Binary Search

– Comparison must be based upon Data type of Input.
– to get Data type of Input
– sizeof( )
Eg:

#define TYPE(x)(
       sizeof(x) == sizeof(integer) ? return integer:
       sizeof(x) == sizeof(character) ? return char:
       sizeof(x) == sizeof(double)) ? return double :
       )

 

– Make function to compare according to size.

Compare (A[mid] , value)
{
if(TYPE(A[mid] != TYPE(value)) return null;

if ( TYPE( A[mid] == integer ){ 
//Simple comparison and return
}

if ( TYPE( A[mid] == character ){
// Compare with User Defined which function which checks for lexicographic order.
//See Void CompareLexo()
}
 
}//
Compare Void CompareLexo(){ 
//pseudo code to understand logic. 
 const char* string = "Bac"; 
 int c = strcmp(string, "Bca");  //c will be <= -1 
 int c = strcmp(string, "B");  //c will be >=1 
}

The same way you can think for Double , Structure etc Input and create your code.

PS : Please comment/mail us perfect running code or better logic

 

Similar Articles

Filed Under: problem Tagged With: algorithm

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

System Design: Designing a LLD for Hotel Booking

Find position of the only set bit

Maximum of all subarrays of size k

Binary Tree Isomorphic to each other

Edit Distance ( Dynamic Programming )

CodeChef’ RRCOPY

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

The greedy coins game Dynamic Programming

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Given a float number convert it into the string WITHOUT using any inbuilt Function

SAP Off Campus Hiring_ March 2015 Sample Questions

C++ OOPs Part2

Singly linked list

Naurki.com Security Breach

Add Sub Multiply very large number stored as string

Printing each word reverse in string

Closed Parentheses checker

Find the smallest window in a string containing all characters of another string

Stickler thief

Convert number to words java

Handle duplicates in Binary Search Tree

Skiing on Mountains Matrix

Linked List V/S Binary Search Tree

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Adobe Interview Questions 8 month Exp

LeetCode : Word Search

LeetCode: Container With Most Water

Reversal of LinkedList

Code Chef PRGIFT Solution

Doubly linked list

Copyright © 2025 · Genesis Framework · WordPress · Log in