lundi 25 janvier 2016

binary search when imputing a value into an array

The question is like this:

Implement a modified version of binary search such that 1. If the key is found in A[], return the smallest index such that A[index] == key.

2. If the key is not found in A[], the function returns the value -(insertionPoint + 1).
   Insertion point is defined as the index at which the key would be inserted into the array.

   Examples:
   A[] = {3, 5, 6, 6, 6, 8}
     key = 2 return value is -1 (insertion point = 0, insert before 3).
     key = 6 return value is 2.
     key = 7 return value is -6 (insertion point = 5, insert before 8).
     key = 9 return value is -7 (insertion point = 6, insert at end of array).     

Aucun commentaire:

Enregistrer un commentaire