Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include "helpers.h"
  4. #include <stdbool.h>
  5.  
  6. bool search( int value, int values[], int n)
  7. {
  8. int lo =0;
  9. int hi=n-1;
  10. int mid=(lo+hi)/2;
  11. int found=0;
  12.  
  13. while (lo <=hi)
  14. {
  15. if (values[mid] == value)
  16. {
  17. found=1;
  18. return true;
  19. }
  20.  
  21. else if (values[mid] < value)
  22. (lo = mid+1);
  23.  
  24. else
  25. {
  26. (hi = mid-1);
  27. mid = ((lo+hi)/2);
  28. }
  29. }
  30. if (found==0)
  31. return false;
  32. else
  33. return true;
  34. }
  35.  
  36.  
  37. void sort(int values[], int n)
  38. {
  39.  
  40. for (int i = 0, swap ; i < ( n - 1 ); i++)
  41. {
  42. for (int j = 0 ; j < n - i - 1; j++)
  43. {
  44. if (values[j] > values[j+1])
  45. {
  46. swap = values[j];
  47. values[j] = values[j+1];
  48. values[j+1] = swap;
  49. }
  50. }
  51.  
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement