Advertisement
Mary_99

bsearch

Jun 1st, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define ARRAY_SIZE 20
  5. #define ELEMENTS 7
  6.  
  7. int compereInt(const void *integer1, const void *integer2);
  8. int compereDouble(const void *doubleValue1, const void *doubleValue2) ;
  9. int compereNames(const void *name1, const void *name2);
  10. void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
  11.  
  12.  
  13. int main()
  14. {
  15.     int integers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  16.     double doubleValues[] = {1.67, 2.32, 4.23, 6.14, 11.57, 13.11, 21.37, 23.23};
  17.     char names[][ARRAY_SIZE]= {"arek", "bartek", "krzys", "ola", "ania"};
  18.    
  19.     int *foundInteger = NULL;
  20.     double *foundDouble = NULL;
  21.     char *foundName = NULL;
  22.     int currentElement;
  23.     int findInteger[ELEMENTS] = {1, 4, 7, 8, 11, 18, 20};
  24.     double findDouble[ELEMENTS] = {2.22,21.37, 6.78, 4.23, 13.12, 23.23, 24.44};
  25.     char findName[ELEMENTS][ARRAY_SIZE] = {"arek", "krystian", "sebastian", "ola", "maciek","bartek", "antek"};
  26.    
  27.     for(currentElement = 0; currentElement < ELEMENTS; currentElement++)
  28.     {
  29.           foundInteger = (int*)bsearch( &findInteger[currentElement], integers, sizeof(integers)/sizeof(int), sizeof(int), compereInt);
  30.           foundDouble = (double*)bsearch(&findDouble[currentElement], doubleValues, sizeof(doubleValues)/sizeof(double), sizeof(double), compereDouble);
  31.           foundName = (char*)bsearch(&findName[currentElement], names, sizeof(names)/sizeof(*names), sizeof(*names), compereNames);
  32.          
  33.           if(foundInteger != NULL)
  34.             printf("Found integer %d\n", findInteger[currentElement]);
  35.           else
  36.             printf("Integer %d not found\n", findInteger[currentElement]);
  37.          
  38.           if(foundDouble != NULL)
  39.             printf("Found double %f\n", findDouble[currentElement]);
  40.           else
  41.             printf("Double %f not found\n", findDouble[currentElement]);
  42.          
  43.           if (foundName!= NULL)
  44.             printf("Found name %s\n\n", findName[currentElement]);
  45.           else
  46.             printf("Name %s not found\n\n", findName[currentElement]);
  47.     }
  48.    
  49.     return 0;
  50. }
  51.  
  52.  
  53. int compereInt(const void *integer1, const void *integer2)
  54. {
  55.     if (*(int*)integer1 == *(int*)integer2)
  56.     {
  57.         return 0;
  58.     }
  59.     else if (*(int*)integer1 > *(int*)integer2)
  60.     {
  61.         return 1;
  62.     }
  63.     else
  64.     {
  65.         return -1;
  66.     }
  67. }
  68.  
  69. int compereDouble(const void *doubleValue1, const void *doubleValue2)
  70. {
  71.     if (*(double*)doubleValue1 == *(double*)doubleValue2)
  72.     {
  73.         return 0;
  74.     }
  75.     else if (*(double*)doubleValue1 > *(double*)doubleValue2)
  76.     {
  77.         return 1;
  78.     }
  79.     else
  80.     {
  81.         return -1;
  82.     }
  83. }
  84.  
  85. int compereNames(const void *name1, const void *name2)
  86. {
  87.     return strcmp(name1, name2);
  88. }
  89.  
  90. void* bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
  91. {
  92.    
  93.    /*
  94.     const void *key Poszukiwany klucz.
  95.     const void *base    Wskaźnik na posortowaną tablicę, która ma zostać przeszukana.
  96.     size_t nmemb    Liczba elementów w tablicy.
  97.     size_t size Liczba bajtów zajmowanych przez jeden element tablicy.
  98.     int (*compare ) ( const void *, const void *)   Funkcja porównująca klucze. Do pierwszego argumentu przedmiotowej funkcji trafia wskaźnik na element poszukiwany key, natomiast do drugiego wskaźnik na element tablicy base z którym ma nastąpić porównanie.*/
  99.    
  100.     const void* currentElement = 0;
  101.     int start = 0;
  102.     int end = nmemb;
  103.     int middle;
  104.     int searchElement;
  105.  
  106.     while (start <= end)
  107.     {
  108.         middle = start + (end - start) / 2;
  109.         currentElement = (char*)base + middle* size;
  110.         searchElement = (*compar)(key, currentElement);
  111.  
  112.         if (searchElement == 0)
  113.         {
  114.             return ((void*)currentElement);
  115.         }
  116.         else if (searchElement < 0)
  117.         {
  118.             end = middle - 1;
  119.         }
  120.         else
  121.         {
  122.             start = middle + 1;
  123.         }
  124.     }
  125.  
  126.     return NULL;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement