Advertisement
Guest User

Untitled

a guest
May 27th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6. int BinS(int *tab, int left, int right, int s);
  7. int main()
  8. {
  9. srand(time(NULL));
  10. int x = 10,temp;
  11.  
  12. int *tab=new int [x];
  13. for (int i=0;i<x;i++)
  14. {
  15. tab[i] = rand()%x+1;
  16. }
  17.  
  18. for(int j=0;j<x;j++)
  19. for (int i=1;i<x;i++)
  20. {
  21. if (tab[i-1] > tab[i])
  22. {
  23. temp=tab[i - 1];
  24. tab[i - 1] = tab[i];
  25. tab[i] = temp;
  26. }
  27. }
  28.  
  29. for (int i = 0; i<x; i++)
  30. {
  31. cout<<tab[i]<<"\t";
  32. }
  33. cout << BinS(tab, 1, (x-1), 5);
  34.  
  35. return 0;
  36. }
  37.  
  38. int BinS(int *tab, int left, int right, int s)
  39. {
  40. int mid;
  41. for (;;)
  42. {
  43. if (left > right)
  44. {
  45. return -1;
  46. }
  47. else
  48. {
  49. mid = (left + right) / 2;
  50. if (mid == s)
  51. {
  52. return mid;
  53. }
  54. else
  55. {
  56. if (tab[mid] < s)
  57. {
  58. left = mid + 1;
  59. }
  60. else
  61. {
  62. right = mid - 1;
  63. }
  64. }
  65. }
  66. }
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement