• 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 the smallest window in a string containing all characters of another string

Amazon Interview On-Campus For Internship – 1

Linked List V/S Binary Search Tree

25 horses 5 tracks Find 3 fastest puzzle

Python List

Convert number to words java

Printing each word reverse in string

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

SAP Off Campus Hiring_ March 2015 Sample Questions

Rectangular chocolate bar Create at least one piece which consists of exactly nTiles tiles

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

Circular Linked List

flattens 2 D linked list to a single sorted link list

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

SAP Hiring Off-Campus General Aptitude

BFS (Breath First Search)

Right view of Binary tree

strtok()

Common Ancestor in a Binary Tree or Binary Search Tree

K’th Largest Element in BST when modification to BST is not allowed

Flipkart Set 1 On Campus with Answers

LeetCode: Binary Tree Maximum Path Sum

Find the kth number with prime factors 3, 5 and 7

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Count Possible Decodings of a given Digit Sequence

Hackerearth : Counting Subarrays

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

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

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

Minimum insertions to form a palindrome

Copyright © 2026 · Genesis Framework · WordPress · Log in