• 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

Find Nearest Minimum number in left side in O(n)

flattens 2 D linked list to a single sorted link list

Adobe Interview Questions 8 month Exp

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Daughter’s Age VeryGood Puzzle

Find position of the only set bit

Regular Expression Matching

Print vertical sum of all the axis in the given binary tree

Generic Object Oriented Stack with Template

VMWare SDEII Interview

Reliance Jio Software Developer Interview Experience

Find Percentage of Words matching in Two Strings

C Program for TAIL command of UNIX

Leetcode: Merge Intervals

Amazon Interview On-Campus For Internship – 1

DFS (Depth First Search)

Knight Tour Problem (Graph – Breadth First Search)

Client Server C program

SAP Off Campus Hiring_ March 2015 Sample Questions

Binary Tree Isomorphic to each other

Closed Parentheses checker

Print Power Set of a Set

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

How Radix sort works

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

Introduction To Number Theory ( Part 1 )

Given Set of words or A String find whether chain is possible from these words or not

Skiing on Mountains Matrix

TicTacToe Game As Asked in Flipkart

Puzzle : 100 doors in a row Visit and Toggle the door. What state the door will be after nth pass ?

Copyright © 2026 · Genesis Framework · WordPress · Log in