• 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

Circular Linked List

SAP Hiring Off-Campus General Aptitude

Common Ancestor in a Binary Tree or Binary Search Tree

Best Java Book | Top Java Programming Book for Beginners

Cisco Hiring Event 21st – 22nd Feb 2015

Leetcode: Merge Intervals

HackeEarth Flipkart’s Drone

Add Sub Multiply very large number stored as string

Python List

Walmart Labs Interview Experience

Search element in a matrix with all rows and columns in sorted order

Maximum of all subarrays of size k

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Amazon Interview On-Campus For Internship – 1

Flipkart SDET Interview Experience

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

Trie Dictionary

C++ OOPs Part1

Generic Object Oriented Stack with Template

Find Percentage of Words matching in Two Strings

strtok()

ADOBE Aptitude C Language Test

Maximum size of square sub matrix with all 1’s in a binary matrix

Python Dictionaries

Find next greater number with same set of digits

Minimum insertions to form a palindrome

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Find the number ABCD such that when multipled by 4 gives DCBA.

Password Predictor

Maximum sum contiguous subarray of an Array

Copyright © 2026 · Genesis Framework · WordPress · Log in