• 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

Adobe Interview Questions 8 month Exp

Wrong Directions given find minimum moves so that he can reach to the destination

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

Find position of the only set bit

Python List

Circular Linked List

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

Print all nodes that are at distance k from a leaf node

Edit Distance ( Dynamic Programming )

Generate largest number arranging a no. of given non negative integer numbers

SAP Hiring Off-Campus General Aptitude

Leetcode: Edit Distance

Templates in C++

Doubly linked list

Count number of ways to reach a given score in a game

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

The Magic HackerEarth Nirvana solutions Hiring Challenge

Binary Tree in Java

Best Java Book | Top Java Programming Book for Beginners

Longest Increasing Subsequence

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Calculate price of parking from parking start end time prices

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Sort Stack in place

Level order traversal in Spiral form

LeetCode: Container With Most Water

Naurki.com Security Breach

Urban Ladder Written Test.

strtok()

Copyright © 2026 · Genesis Framework · WordPress · Log in