Last active
November 8, 2016 04:20
-
-
Save eduardoaugustojulio/129662b2c89b1c21b2e6aa02c3fdf561 to your computer and use it in GitHub Desktop.
binary search c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int compare_int(const void * a, const void * b) | |
{ | |
return (*(int*)a - *(int*)b); | |
} | |
int var_list[] = { 50, 20, 60, 40, 10, 30 }; | |
int main() | |
{ | |
int * pItem; | |
qsort (var_list, (sizeof(var_list) / sizeof(int)), sizeof (int), compare_int); | |
pItem = (int*) bsearch (40, values, (sizeof(var_list) / sizeof(int)), sizeof (int), compare_int); | |
if (pItem!=NULL) | |
printf ("%d is in the array.\n",*pItem); | |
else | |
printf ("%d is not in the array.\n",key); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment