Binary Search.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Search integer values using a binary search.
  2. #define USE_TI89 // Compile for TI-89
  3. #define USE_TI92PLUS // Compile for TI-92 Plus
  4. #define USE_V200 // Compile for V200
  5. #define OPTIMIZE_ROM_CALLS
  6. #define MIN_AMS 100 // Compile for AMS 1.00 or higher
  7. #define SAVE_SCREEN // Save/Restore LCD Contents
  8. #include <tigcclib.h> // Include All Header Files
  9. // Comparison Function
  10. CALLBACK short int_comp(const void *a, const void *b)
  11. {
  12. return (*(const short*)a) - (*(const short*)b);
  13. }
  14. // Main Function
  15. void _main(void)
  16. {
  17. short list[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  18. short i;
  19. short font;
  20. void *p;
  21. font = FontGetSys();
  22. FontSetSys(F_4x6);
  23. clrscr ();
  24. for (i = -1; i < 11; i++) {
  25. p = bsearch (&i, list, sizeof(list)/sizeof(list[0]), sizeof (list[0]), int_comp);
  26. if (p == NULL) {
  27. printf ("%d not found\n", i);
  28. }
  29. else {
  30. printf ("%d is at index %lu\n", i, ((void *)p - (void *)list)/sizeof(list[0]));
  31. }
  32. }
  33. FontSetSys(font);
  34. ngetchx ();
  35. }