Advertisement
jbn6972

Untitled

Dec 8th, 2023
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // binary_search - 2 way search (left,right)
  4. // idx - 0 1 2 3 4 5 6 7 8 9
  5. // arr - 1 2 3 4 5 6 7 8 9 10
  6.  
  7. // Sample 0
  8. // key - 15
  9. // n - 10
  10.  
  11. // l_idx = 0
  12. // r_idx = 9
  13. // m_dix = 5
  14.  
  15. // arr[5] = 6
  16. // 15 - 6 -> 15 is greater than 6 search right side
  17. // l_idx = m_idx = 5
  18. // r_idx = 10
  19.  
  20. // m_idx = 7
  21. // arr[7] = 8
  22.  
  23. // 15 - 8 -> 15 is greater than 8 , search on right side
  24. // l_idx = 7
  25. // r_idx = 10
  26. // m_idx = 8
  27.  
  28. // arr[8] = 9
  29. // 15 - 9 -> 15 is greater than 9, search on right side
  30. // l_idx = 8
  31. // r_idx = 10
  32. // m_idx = 9
  33.  
  34. // arr[9] = 10
  35. // 15 - 10 -> 15 is greater than 10, search on right side
  36. // l_idx = 9
  37. // r_idx = 10
  38. // m_idx = 9
  39.  
  40. // arr[9] = 10
  41. // 15 - 10 -> 15 is greater than 10, search on right side
  42.  
  43. // l_idx == r_idx - the element is not there so break the loop
  44.  
  45. // Sample 1
  46. // key - 5
  47. // n - 10
  48.  
  49. // l_idx = 0
  50. // r_idx = 10
  51. // m_idx = 5
  52.  
  53. // arr[5] = 6
  54. // 5 - 6 -> 6 greater than 5 so search in the left side
  55. // r_idx = m_idx = 5
  56. // l_idx = 0
  57.  
  58. // m_idx = (0+5) / 2 = 2
  59. // arr[2] = 3
  60. // 5 - 3 -> 3 smaller than 5 so search in the right side
  61. // l_idx = m_idx = 2
  62. // r_idx = 5
  63.  
  64. // m_idx = (2+5) / 2 = 3
  65. // arr[3] = 4
  66. // 5 - 4 -> 4 smaller than 5 so search in the right side
  67. // l_idx = 3
  68. // r_idx = 5
  69.  
  70. // m_idx = (5+3) / 2 = 4
  71. // arr[4] = 5
  72.  
  73. // 5 - 5 -> Element found
  74.  
  75. // Sample 2
  76. // key - 8
  77. // n - 10
  78.  
  79. // l_idx = 0
  80. // r_idx = n = 10
  81. // m_idx = (l_idx + r_idx) / 2 = 5
  82.  
  83. // 8 - 6  6 smaller so search on right side of the array
  84.  
  85. // l_idx = m_idx = 5
  86. // r_idx = 10
  87.  
  88. // m_idx = (5+10) / 2 = 7
  89. // arr[7] = 8  - 8 8 is equal found our element
  90.  
  91. // recursion - calling a function within itself (infinite loop)
  92.  
  93. //  write a c program using binary search if element is present return its index else return -1
  94.  
  95. int binarySearch(int arr[], int l, int r, int key)
  96. {
  97.     while (l < r)
  98.     {
  99.         int m = (l + (r-1)) / 2;
  100.         // check if key is present at mid
  101.         if (arr[m] == key)
  102.         {
  103.             return m;
  104.         }
  105.  
  106.         //  if key is greater, ignore the left half
  107.         if (arr[m] < key)
  108.         {
  109.             l = m + 1;
  110.         }
  111.         else
  112.         // if key is smaller, ignore the right half
  113.         {
  114.             r = m - 1;
  115.         }
  116.     }
  117.     return -1;
  118. }
  119.  
  120. int main()
  121. {
  122.     int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  123.     int key = 5;
  124.  
  125.     int result = binarySearch(arr, 0, 9, key);
  126.  
  127.     if (result != -1)
  128.     {
  129.         printf("Key is present in index :: %d", result);
  130.     }
  131.     else
  132.     {
  133.         printf("Key is not present");
  134.     }
  135.  
  136.     return 0;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement