Binary Search in C

CODE:1-BINARY SEARCH

#include <stdio.h>
#include <stdlib.h>

int values[] = { 1 , 2 , 3, 4 , 9 , 10 };

int compare (const void * a, const void * b) {
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int *pos;
  int key = 9;
  
  pos = (int*) bsearch (&key, values, 6, sizeof (int), compare);
  
  if ( pos != NULL )
    printf ("%d is in the array", *pos);
  else
    printf ("%d is not in the array", key);
  
  return 0;
}

Comments

Popular posts from this blog

Error : DependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: com.adobe.aem:uber-jar:jar:apis -> version 6.3.0 vs 6.4.0

Operators in Asterisk with Linux

ERROR Exception while handling event Sitecore.Eventing.Remote.PublishEndRemoteEventException: System.AggregateExceptionMessage: One or more exceptions occurred while processing the subscribers to the 'publish:end:remote'