{"id":22598,"date":"2020-11-10T10:41:24","date_gmt":"2020-11-10T05:11:24","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/"},"modified":"2024-09-03T11:24:36","modified_gmt":"2024-09-03T05:54:36","slug":"binary-search-algorithm","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/","title":{"rendered":"Binary Search Algorithm | What is Binary Search?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"what-is-binary-search\"><strong>What is Binary Search?<\/strong><\/h2>\n\n\n\n<p>Binary Search Algorithm is one of the widely used searching techniques. It can be used to sort arrays. This searching technique follows the divide and conquer strategy. The search space always reduces to half in every iteration.<\/p>\n\n\n\n<p>Binary Search Algorithm is a very efficient technique for searching but it needs some order on which partition of the array will occur.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"advantages-of-binary-search-algorithm\"><strong>Advantages of Binary Search Algorithm<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Since it follows the technique to eliminate half of the array elements, it is more efficient as compared to linear search for large data.<\/li>\n\n\n\n<li>Better time complexity and thus takes less compilation time.&nbsp;<\/li>\n\n\n\n<li>An improvement over linear search as it breaks the array down in half rather than sequentially traversing through the array elements.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"limitations-of-binary-search-algorithm\"><strong>Limitations of Binary Search Algorithm<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Binary Search algorithm could only be implemented over a sorted array.&nbsp;<\/li>\n\n\n\n<li>Small unsorted arrays would take considerate time in sorting and then searching the desired element. So, binary search is not preferred in such cases.<\/li>\n\n\n\n<li>It has poor locality of reference compared to linear search algorithm when comes to in-memory searching for short intervals.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"applications-of-binary-search\"><strong>Applications of Binary Search<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>This algorithm is used to search element in a given sorted array with more efficiency.<\/li>\n\n\n\n<li>It could also be used for few other additional operations like- to find the smallest element in the array or to find the largest element in the array.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-pseudocode\"><strong>Binary Search Pseudocode<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We are given an input array that is supposed to be sorted in ascending order.<\/li>\n\n\n\n<li>We take two variables which will act as a pointer i.e, beg, and end.<\/li>\n\n\n\n<li>Beg will be assigned with 0 and the end will be assigned to the last index of the array.<\/li>\n\n\n\n<li>Now we will introduce another variable mid which will mark the middle of the current array. That will be computed as (low+high)\/2.<\/li>\n\n\n\n<li>If the element present at the mid index is equal to the element to be searched, then just return the mid index.<\/li>\n\n\n\n<li>If the element to be searched is smaller than the element present at the mid index, move end to mid-1, and all RHS will get discarded.<\/li>\n\n\n\n<li>If the element to be searched is greater than the element present at the mid index, move beg to mid+1, and all LHS will get discarded.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-algorithm\"><strong>Binary Search Algorithm<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"iterative-approach\"><strong>Iterative approach<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>binarySearch(arr, size)\n\t\t   loop until beg is not equal to end\n    midIndex = (beg + end)\/2\n    if (item == arr&#91;midIndex] )\n        return midIndex\n    else if (item &gt; arr&#91;midIndex] ) \n        beg = midIndex + 1\n    else                       \n        end = midIndex - 1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"recursive-approach\"><strong>Recursive approach<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>binarySearch(arr, item, beg, end)\n    if beg&lt;=end\n        midIndex = (beg + end) \/ 2 \n        if item == arr&#91;midIndex]\n            return midIndex\n        else if item &lt; arr&#91;midIndex]        \n            return binarySearch(arr, item, midIndex + 1, end)\n        else                              \n            return binarySearch(arr, item, beg, midIndex - 1)\n    \n    return -1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"binary-search-algorithm-dry-run\"><strong>Binary Search Algorithm Dry Run<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Item to be searched=20<\/li>\n<\/ol>\n\n\n\n<p>input:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td>16<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>beg=0, end=4, mid=2\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td><strong>2<\/strong><\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td><strong>16<\/strong><\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>beg=3, end=4, mid=3\t<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td><strong>3<\/strong><\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td>16<\/td><td><strong>20<\/strong><\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Element found at index 3, Hence 3 will get returned.<\/p>\n\n\n\n<p>2. Given is the pictorial representation of binary search through an array of size 6 arranged in ascending order. We intend to search the element 78:<\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-2.png\"><img decoding=\"async\" width=\"1012\" height=\"857\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-2.png\" alt=\"Binary search algorithm\" class=\"wp-image-35276\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-2.png 1012w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-2-300x254.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-2-768x650.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-2-696x589.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-2-496x420.png 496w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/06\/image-2-150x127.png 150w\" sizes=\"(max-width: 1012px) 100vw, 1012px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-time-complexity\"><strong>Binary Search Time Complexity<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In each iteration, the search space is getting divided by 2. That means that in the current iteration you have to deal with half of the previous iteration array.<\/li>\n\n\n\n<li>And the above steps continue till beg&lt;end<\/li>\n\n\n\n<li>Best case could be the case where the first mid-value get matched to the element to be searched<\/li>\n\n\n\n<li>Best Time Complexity: O(1)<\/li>\n\n\n\n<li>Average Time Complexity: O(logn)<\/li>\n\n\n\n<li>Worst Time Complexity: O(logn)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"calculating-time-complexity-of-binary-search\"><strong>Calculating Time complexity of binary search<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Let k be the number of iterations.&nbsp;<\/li>\n<\/ul>\n\n\n\n<p>(E.g. If a binary search gets terminated after four iterations, then k=4.)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In a binary search algorithm, the array taken gets divided by half at every iteration.<\/li>\n\n\n\n<li>If n is the length of the array at the first iteration, then at the second iteration, the length of the array will be n\/2<\/li>\n\n\n\n<li>Again dividing by half in the third iteration will make the array's length = (n\/2)\/2=n\/(2^k).<\/li>\n\n\n\n<li>Similarly, at the fourth iteration, the value of the array's length will be n\/(2^3). and so on.<\/li>\n\n\n\n<li>At the kth iteration, the value of the length of the array will be = (n\/2^k).<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As after k divisions, the length of the array becomes 1 therefore,<\/li>\n<\/ul>\n\n\n\n<p>n\/(2^k)=1<\/p>\n\n\n\n<p>=&gt; n = 2k<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Applying log function on both sides:<\/li>\n<\/ul>\n\n\n\n<p>=&gt;log(n) = log&nbsp;(2^k)        (log with base 2)<\/p>\n\n\n\n<p>=&gt;log&nbsp;(n) = k log&nbsp;(2)        (log with base 2)<\/p>\n\n\n\n<p>As (log (a) = 1)                 (log with base 2)<\/p>\n\n\n\n<p>Therefore,<\/p>\n\n\n\n<p>=&gt; k = log&nbsp;(base 2) (n)<\/p>\n\n\n\n<p>Therefore, the time complexity of the Binary Search algorithm is log (base 2) n.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-space-complexity\"><strong>Binary Search Space Complexity<\/strong><\/h2>\n\n\n\n<p>No auxiliary space is required in Binary Search implementation<\/p>\n\n\n\n<p>The binary search algorithm's space complexity depends on the way the algorithm has been implemented. Two ways in which it can be implemented are:<\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li><strong>Iterative method<\/strong>: In this method, the iterations are controlled through looping conditions. The space complexity of binary search in the iterative method is O(1).<\/li>\n<\/ol>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\" start=\"2\">\n<li><strong>Recursive method<\/strong>: In this method, there is no loop, and the new values are passed to the next recursion of the loop. Here, the max and min values are used as the boundary condition. The space complexity of binary search in the recursive method is O(log n).<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-in-c\"><strong>Binary Search in C&nbsp;<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"iterative-binary-search-in-c\"><strong>Iterative Binary Search in C<\/strong><\/h4>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\nint binarySearch( int arr&#91;], int item, int beg, int end) {\n  while (beg &lt;= end) {\n    int midIndex = beg + (end - beg) \/ 2;\n    if (arr&#91;midIndex] == item)\n      return midIndex;\n    if (arr&#91;midIndex] &gt; item)\n      beg = midIndex + 1;\n    else\n      end = midIndex - 1;\n  }\n  return -1;\n}\nint main(void) {\n  int arr&#91;] = {13, 45, 65, 16, 117, 82, 19};\n  int n = sizeof(arr) \/ sizeof(arr&#91;0]);\n  int item = 45;\n  int ans = binarySearch(arr, item, 0, n - 1);\n  if (ans == -1)\n    printf(\"Element not present\");\n  else\n    printf(\"answer: %d\", ans);\n  return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>answer: 1<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"recursive-binary-search-in-c\"><strong>Recursive Binary Search in C<\/strong><\/h4>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\nint binarySearch(int arr&#91;], int item, int beg, int end) {\n  if (end &gt;= beg) {\n    int midIndex = beg + (end - beg) \/ 2;\n    if (arr&#91;midIndex] == item)\n      return midIndex;\n    if (arr&#91;midIndex] &lt; item)\n      return binarySearch(arr, item, beg, midIndex - 1);\n    return binarySearch(arr, item, midIndex + 1, end);\n  }\n  return -1;\n}\nint main(void) {\n  int arr&#91;] = {13, 45, 65, 16, 117, 82, 19};\n  int n = sizeof(arr) \/ sizeof(arr&#91;0]);\n  int item = 45;\n  int ans = binarySearch(arr, item, 0, n - 1);\n  if (ans == -1)\n    printf(\"Element not present\");\n  else\n    printf(\"answer: %d\", ans);\n  return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p>answer: 1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-in-java\"><strong>Binary Search in JAVA<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"iterative-binary-search-in-java\"><strong>Iterative Binary Search in JAVA<\/strong><\/h4>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class BinarySearch {\n  int binarySearch(int arr&#91;], int item, int beg, int end) {\n    while (beg &lt;= end) {\n      int midIndex = beg + (end - beg) \/ 2;\n      if (arr&#91;midIndex] == item)\n        return midIndex;\n      if (arr&#91;midIndex] &gt; item)\n        beg = midIndex + 1;\n      else\n        end = midIndex - 1;\n    }\n    return -1;\n  }\n\n  public static void main(String args&#91;]) {\n    BinarySearch ob = new BinarySearch();\n    int arr&#91;] = {13, 45, 65, 16, 117, 82, 19};\n    int n = arr.length;\n    int item = 45;\n    int ans = ob.binarySearch(arr, item, 0, n - 1);\n    if (ans == -1)\n      System.out.println(\"Element not present\");\n    else\n      System.out.println(\"answer: \"+ans);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>answer: 1<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"recursive-binary-search-in-java\"><strong>Recursive Binary Search in JAVA<\/strong><\/h4>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class BinarySearch {\n    int binarySearch(int arr&#91;], int item, int beg, int end) {\n    if (end &gt;= beg) {\n      int midIndex = beg + (end - beg) \/ 2;\n      if (arr&#91;midIndex] == item)\n        return midIndex;\n      if (arr&#91;midIndex] &lt; item)\n        return binarySearch(arr, item, beg, midIndex - 1);\n      return binarySearch(arr, item, midIndex + 1, end);\n    }\n    return -1;\n  }\n\n  public static void main(String args&#91;]) {\n    BinarySearch ob = new BinarySearch();\n    int arr&#91;] = {13, 45, 65, 16, 117, 82, 19};\n    int n = arr.length;\n    int item = 45;\n    int ans = ob.binarySearch(arr, item, 0, n - 1);\n    if (ans == -1)\n      System.out.println(\"Element not present\");\n    else\n      System.out.println(\"answer: \"+ans);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>answer: 1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-in-c\"><strong>Binary Search in C++&nbsp;<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"iterative-binary-search-in-c\"><strong>Iterative Binary Search in C++&nbsp;<\/strong><\/h4>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\nint binarySearch(int arr&#91;], int item, int beg, int end) {\n  while (beg &lt;= end) {\n    int midIndex = beg + (end - beg) \/ 2;\n    if (arr&#91;midIndex] == item)\n      return midIndex;\n    if (arr&#91;midIndex] &gt; item)\n      beg = midIndex + 1;\n    else\n      end = midIndex - 1;\n  }\n  return -1;\n}\n\nint main(void) {\n  int arr&#91;] = {13, 45, 65, 16, 117, 82, 19};\n  int n = sizeof(arr) \/ sizeof(arr&#91;0]);\n  int item = 45;\n  int ans = binarySearch(arr, item, 0, n - 1);\n  if (ans == -1)\n    printf(\"Element not present\");\n  else\n    printf(\"answer: %d\", ans);\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>answer: 1<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"recursive-binary-search-in-c\"><strong>Recursive Binary Search in C++<\/strong><\/h4>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint binarySearch(int array&#91;], int item, int beg, int end) {\n  if (end &gt;= beg) {\n    int midIndex = beg + (end - beg) \/ 2;\n\n    if (array&#91;midIndex] == item)\n      return midIndex;\n\n    if (array&#91;midIndex] &lt; item)\n      return binarySearch(array, item, beg, midIndex - 1);\n\n    return binarySearch(array, item, midIndex + 1, end);\n  }\n\n  return -1;\n}\n\nint main(void) {\n  int arr&#91;] = {13, 45, 65, 16, 117, 82, 19};\n  int n = sizeof(arr) \/ sizeof(arr&#91;0]);\n  int item = 45;\n  int ans = binarySearch(arr, item, 0, n - 1);\n  if (ans == -1)\n    printf(\"Element not present\");\n  else\n    printf(\"answer: %d\", ans);\n}<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>answer: 1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-in-python\"><strong>Binary Search in Python<\/strong><\/h2>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"iterative-binary-search-in-python\"><strong>Iterative Binary Search in Python<\/strong><\/h4>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def binarySearch(arr, item, beg, end):\n    while beg &lt;= end:\n        mid = beg + (end - beg)\/\/2\n        if arr&#91;mid] == item:\n            return mid\n        elif arr&#91;mid] &gt; item:\n            beg = mid + 1\n        else:\n            end = mid - 1\n    return -1\n\narr = &#91;13, 45, 65, 16, 117, 82, 19]\nitem = 45\n\nans = binarySearch(arr, item, 0, len(arr)-1)\n\nif ans != -1:\n    print(\"answer: \" + str(ans))\nelse:\n    print(\"Element not present\")<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>answer: 1<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"recursive-binary-search-in-python\"><strong>Recursive Binary Search in Python<\/strong><\/h4>\n\n\n\n<p><strong>Code:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def binarySearch(arr, item, beg, end):\n    if end &gt;= beg:\n        midIndex = beg + (end - beg)\/\/2\n        if arr&#91;midIndex] == item:\n            return midIndex\n        elif arr&#91;midIndex] &lt; item:\n            return binarySearch(arr, item, beg, midIndex-1)\n        else:\n            return binarySearch(arr, item, midIndex + 1, end)\n    else:\n        return -1\n\narr = &#91;13, 45, 65, 16, 117, 82, 19]\nitem = 45\n\nans = binarySearch(arr, item, 0, len(arr)-1)\n\nif ans != -1:\n    print(\"answer: \" + str(ans))\nelse:\n    print(\"Element not present\")<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><br>answer: 1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"binary-search-example\"><strong>Binary Search Example<\/strong><\/h2>\n\n\n\n<p><strong>Example1:<\/strong><\/p>\n\n\n\n<p>Your task is to find the first and last occurrence of the given element in the array. If an element is not present, return -1. Assuming sorting here is in ascending order.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>Input<\/p>\n\n\n\n<p>7<\/p>\n\n\n\n<p>1 2 5 5 5 5 6&nbsp;<\/p>\n\n\n\n<p>5<\/p>\n\n\n\n<p>Output<\/p>\n\n\n\n<p>2 5<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"code-in-java\">Code in Java:<br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\nclass Demo {\n\tpublic static void main ( String&#91;] args ) {\n\t\t    Scanner sc = new Scanner( System.in );\n\t\t    int n = sc.nextInt();\n\t\t    int&#91;] arr = new int&#91;n];\n\t\t    for( int i = 0 ; i &lt; n ; i++ )\n\t\t    {\n\t\t        arr&#91;i] = sc.nextInt();\n\t\t    }\n\t\t    int item = sc.nextInt();\n\t\t    int beg = 0, end = 0;\n\t\t    for( int i = 0 ; i &lt; n ; i++ )\n\t\t    {\n\t\t        if( arr&#91;i] == item )\n\t\t        {\n\t\t            beg = i;\n\t\t            break;\n\t\t        }\n\t\t        else{\n\t\t            beg = -1;\n\t\t        }\n\t\t    }\n\t\t    for( int i = arr.length - 1 ; i &gt; 0; i-- )\n\t\t    {\n\t\t        if( arr&#91;i] == item )\n\t\t        {\n\t\t            end = i;\n\t\t            break;\n\t\t        }\n\t\t    }\n\t\t    if(beg == -1)\n\t\t    {\n\t\t        System.out.println( -1 );\n\t\t    }\n\t\t    else {\n\t\t        System.out.println( beg + \" \" + end );\n\t\t    }\t    \n\t}\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"code-in-c\">Code in C++: <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\n\nint main(void) {\n           \n            int n;\n            cin&gt;&gt;n;\n            int arr&#91;n];\n            for( int i = 0 ; i &lt; n; i++)\n              cin&gt;&gt;arr&#91;i];\n            \n            int item;\n            cin&gt;&gt;item;\n            int beg = 0, end = 0;\n\t\t    for( int i = 0 ; i &lt; n ; i++ )\n\t\t    {\n\t\t        if( arr&#91;i] == item )\n\t\t        {\n\t\t            beg = i;\n\t\t            break;\n\t\t        }\n\t\t        else{\n\t\t            beg = -1;\n\t\t        }\n\t\t    }\n\t\t    for( int i = n - 1 ; i &gt; 0; i-- )\n\t\t    {\n\t\t        if( arr&#91;i] == item )\n\t\t        {\n\t\t            end = i;\n\t\t            break;\n\t\t        }\n\t\t    }\n\t\t    if(beg == -1)\n\t\t    {\n\t\t        cout&lt;&lt;\"-1\";\n\t\t    }\n\t\t    else {\n\t\t        cout&lt;&lt;beg&lt;&lt;\" \"&lt;&lt;end;\n\t\t    }\t\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"code-in-c\">Code in C: <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nint main() {\n            int n;\n            scanf(\"%d \", &amp;n);\n            int arr&#91;n];\n            for( int i = 0 ; i &lt; n; i++)\n              scanf(\"%d \",&amp;arr&#91;i]);\n            \n            int item;\n            scanf(\"%d \",&amp;item);\n            int beg = 0, end = 0;\n\t\t    for( int i = 0 ; i &lt; n ; i++ )\n\t\t    {\n\t\t        if( arr&#91;i] == item )\n\t\t        {\n\t\t            beg = i;\n\t\t            break;\n\t\t        }\n\t\t        else{\n\t\t            beg = -1;\n\t\t        }\n\t\t    }\n\t\t    for( int i = n - 1 ; i &gt; 0; i-- )\n\t\t    {\n\t\t        if( arr&#91;i] == item )\n\t\t        {\n\t\t            end = i;\n\t\t            break;\n\t\t        }\n\t\t    }\n\t\t    if(beg == -1)\n\t\t    {\n\t\t        printf(\"%d \",-1);\n\t\t    }\n\t\t    else {\n\t\t        printf(\"%d %d\",beg,end);\n\t\t    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"complexity-of-searching-insertion-and-deletion-in-binary-tree\"><strong>Complexity of Searching, Insertion and Deletion in Binary Tree<\/strong><\/h2>\n\n\n\n<p>A tree is binary when a node can have at most two children.<\/p>\n\n\n\n<p>Let\u2019s define the complexity of searching, insertion &amp; deletion in a <a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/binary-trees\" target=\"_blank\" rel=\"noreferrer noopener\">binary tree<\/a> with an example.<\/p>\n\n\n\n<p>E.g. The figure given below is a binary tree.<\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-8.png\"><img decoding=\"async\" width=\"223\" height=\"149\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-8.png\" alt=\"Binary search algorithm\" class=\"wp-image-32446\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-8.png 223w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-8-150x100.png 150w\" sizes=\"(max-width: 223px) 100vw, 223px\" \/><\/figure>\n\n\n\n<p>If we have to search for element 7, we will have to traverse all the elements to reach that element, therefore to perform <strong>searching<\/strong> in a binary tree, the worst-case complexity= O(n).<\/p>\n\n\n\n<p>If we have to insert an element as the left child of element 7, we will have to traverse all the elements, therefore performing&nbsp;<strong>insertion<\/strong>&nbsp;in a binary tree, the worst-case complexity= O(n).<\/p>\n\n\n\n<p>If we have to delete element 7, we will have to traverse all the elements to find it, therefore performing&nbsp;<strong>deletion<\/strong>&nbsp;in a binary tree, the worst-case complexity= O(n).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"complexity-of-searching-insertion-and-deletion-in-binary-search-tree-bst\">Complexity of Searching, Insertion and Deletion in Binary Search Tree (BST)<\/h2>\n\n\n\n<p>Binary Search Tree (BST) is considered as a special type of binary tree where the values are arranged in the following order: &nbsp;left child &lt; parent node &lt; right child.<\/p>\n\n\n\n<p>Let\u2019s define the complexity of searching, insertion &amp; deletion in a binary search tree with an example.<\/p>\n\n\n\n<p>E.g. The figure given below is a binary search tree.<\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-9.png\"><img decoding=\"async\" width=\"245\" height=\"173\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-9.png\" alt=\"Binary search algorithm\" class=\"wp-image-32447\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-9.png 245w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-9-100x70.png 100w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-9-150x106.png 150w\" sizes=\"(max-width: 245px) 100vw, 245px\" \/><\/figure>\n\n\n\n<p>If we have to search for element 3, will have to traverse all the elements to reach that element, therefore to perform <strong>searching<\/strong> in a binary search tree, the worst-case complexity= O(n). In general, the time complexity is O(h) where h = height of binary search tree.<\/p>\n\n\n\n<p>If we have to insert an element 2, we will have to traverse all the elements to insert it as the left child of 3. Therefore, to perform&nbsp;<strong>insertion<\/strong>&nbsp;in a binary search tree, the worst-case complexity= O(n) whereas the time complexity in general = O(h).<\/p>\n\n\n\n<p>Suppose we have to delete the element 3. In that case, we will have to traverse all the elements to find it, therefore performing&nbsp;<strong>deletion<\/strong>&nbsp;in a binary tree, the worst-case complexity= O(n) whereas the time complexity in general = O(h).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"complexity-of-searching-insertion-and-deletion-in-avl-tree\">Complexity of Searching, Insertion and Deletion in AVL Tree<\/h2>\n\n\n\n<p>AVL(<strong>Adelson-Velskii and Landis)<\/strong> tree is a binary search tree which is also known as height balanced tree as it has a property in which the difference between the height of the left sub-tree and the right sub-tree can never be more than 1.<\/p>\n\n\n\n<p>Let\u2019s define the complexity of searching, insertion &amp; deletion in AVL tree with an example.<\/p>\n\n\n\n<p>E.g. The figure given below is an AVL tree.<\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-10.png\"><img decoding=\"async\" width=\"334\" height=\"201\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-10.png\" alt=\"Binary search algorithm\" class=\"wp-image-32448\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-10.png 334w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-10-300x181.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-10-150x90.png 150w\" sizes=\"(max-width: 334px) 100vw, 334px\" \/><\/figure>\n\n\n\n<p>If we have to search for element 5, will have to traverse all the elements to reach that element i.e. in the order 30,14,5 = 3 = log n&nbsp;, therefore to perform <strong>searching<\/strong> in an AVL tree, the worst-case complexity= O(log n).<\/p>\n\n\n\n<p>If we have to insert an element 92, we will have to traverse all the elements in order 30, 42, 71 to insert it as the right child of 71. Therefore, to perform <strong>insertion<\/strong> in an AVL tree, the worst-case complexity= O(log n).<\/p>\n\n\n\n<p>If we have to delete the element 71, we will have to traverse all the elements to find it in the order 30, 42, 71. Therefore to perform <strong>deletion<\/strong> in an AVL tree, the worst-case complexity= O(log n).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"big-o-for-binary-search\"><strong>Big O for Binary Search<\/strong><\/h2>\n\n\n\n<p>Big O notation shows the number of operations that an algorithm will perform. It finds the upper bound that gives the worst-case running time complexity. It helps you understand how fast an algorithm is growing and allows you to compare the growth with others.<\/p>\n\n\n\n<p>When the number of steps are counted, you divide the range into half and then process it. This process stops once both the upper and the lower bounds cross. This way you check the entry every time as you have half of the number of entries to check each time. Putting this into a formula as a recurrence T(n) we get:<\/p>\n\n\n\n<p>T(n)=T(n\/2)+1 (Binary search recurrence relation)<\/p>\n\n\n\n<p>With base case T(1)=1.<\/p>\n\n\n\n<p>By applying any variant of the Master Theorem, you get<\/p>\n\n\n\n<p>T(n) \u2208 O(log&nbsp;(n))<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"running-time-of-binary-search\"><strong>Running Time of Binary Search<\/strong><\/h2>\n\n\n\n<p>Binary search halves the size of portion that is reasonable after every incorrect guess. If binary search makes a wrong guess, the portion of the array that contains reasonable guesses is reduced to half. For eg. if the portion had 40 elements then the incorrect guess comes down to 20.<\/p>\n\n\n\n<p>If we start performing binary search on an array of 16 elements then the incorrect guessed will be reduced to length 8, then 4, then 2 and then 1. No more guesses occur once the reasonable portion comes down to just 1. 1 portion element can either be correct or incorrect and we have reached our result. So, for an array of length 16 elements, binary search requires five guesses at most.<\/p>\n\n\n\n<p>Similarly for 32 elements, binary search will require at most 6 guesses to get the answer. We can see a pattern being formed here i.e., we require one more guess to get the result if the number of elements is doubled.<\/p>\n\n\n\n<p>Let there be m number of guesses for an array of length n, then for an array of length 2n, the length cut down after the 1st guess will be n. Now, the number of guesses will become m+1.<\/p>\n\n\n\n<p>(Taking up the general case of an array of length n, we can say that the number of time we are repeatedly reducing the length of n to half until we reach the value 1, plus one. We can write this mathematically with the base-2 logarithm of n.)<\/p>\n\n\n\n<p>The worst case in binary search is achieved when the integers are equal which can be significant when the encoding lengths are large, like large integer types or long strings. This would make the comparison between elements expensive, also comparing the floating-point values are often more expensive than the comparison done between integers or short strings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rb-tree\"><strong>RB Tree<\/strong><\/h2>\n\n\n\n<p>RB Tree (Red Black Tree) is a self-balancing binary search tree where left child &lt; parent &lt; right child. Each node in the RB tree is either red or black in color. These colors ensure that the tree maintains its balance when insertion or deletion of nodes is performed.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"properties\"><strong>Properties:<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\">\n<li>Color of the root node is always black.<\/li>\n\n\n\n<li>Parent node of red color does not contain a red child.<\/li>\n\n\n\n<li>Every leaf node\/NIL is of black color.<\/li>\n\n\n\n<li>The descendent path from every node have the same black height. (Black Height Rule)<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"time-complexity-of-rb-tree\"><strong>Time Complexity of RB Tree<\/strong><\/h4>\n\n\n\n<p>For searching, the time complexity is O(log n). <\/p>\n\n\n\n<p>For insertion, the time complexity is O(log n).<\/p>\n\n\n\n<p>For deletion, the time complexity is O(log n).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"rotation-operation-on-rb-tree\"><strong>Rotation Operation on RB Tree<\/strong><\/h4>\n\n\n\n<p><strong>LR (Left Right) Rotation<\/strong><\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11.png\"><img decoding=\"async\" width=\"1024\" height=\"312\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-1024x312.png\" alt=\"Binary search algorithm\" class=\"wp-image-32451\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-1024x312.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-300x91.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-768x234.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-1536x468.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-696x212.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-1068x326.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-1377x420.png 1377w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11-150x46.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-11.png 1682w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>RR (Right Right) Rotation<\/strong><\/p>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12.png\"><img decoding=\"async\" width=\"1024\" height=\"287\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-1024x287.png\" alt=\"Binary search algorithm\" class=\"wp-image-32452\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-1024x287.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-300x84.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-768x215.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-1536x431.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-696x195.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-1068x299.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-1498x420.png 1498w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12-150x42.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-12.png 1659w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"insertion-in-rb-tree\"><strong>Insertion in RB Tree<\/strong><\/h4>\n\n\n\n<p>Algorithm:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RB-INSERT (T, z)\n 1. y \u2190 nil &#91;T]\n 2. x \u2190 root &#91;T]\n 3. while x \u2260 NIL &#91;T]\n 4. do y \u2190 x\n 5. if key &#91;z] &lt; key &#91;x]\n 6. then x  \u2190 left &#91;x]\n 7. else x \u2190  right &#91;x]\n 8. p &#91;z] \u2190 y\n 9. if y = nil &#91;T]\n 10. then root &#91;T] \u2190 z\n 11. else if key &#91;z] &lt; key &#91;y]\n 12. then left &#91;y] \u2190 z\n 13. else right &#91;y] \u2190 z\n 14. left &#91;z] \u2190 nil &#91;T]\n 15. right &#91;z] \u2190 nil &#91;T]\n 16. color &#91;z] \u2190 RED\n 17. RB-INSERT-FIXUP (T, z)<\/code><\/pre>\n\n\n\n<p>After inserting a node in RB Tree, the color of the nodes are fixed using the following algorithm to maintain the balance of the tree.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RB_INSERT_FIXUP(T,z)\n1. While color &#91;p&#91;z]] = RED\n2.   Do if p&#91;z] = left &#91;p&#91;p&#91;z]]\n3.     then y \u2190 right &#91;p&#91;p&#91;z]]]\n4.       if color &#91;y] = RED\n5.         then color &#91;p&#91;z]] \u2190 BLACK      \/\/Case 1\n6.           color &#91;y] \u2190 BLACK            \/\/Case 1\n7.           color &#91;p&#91;p&#91;z]]] \u2190 RED        \/\/Case 1\n8.\t     Z \u2190 p&#91;p&#91;z]]                  \/\/Case 1\n9.\t else if z = right &#91;p&#91;z]]\n10.\t   then z \u2190 p&#91;z]                  \/\/Case 2\n11.\t     LEFT_ROTATE(T,z)             \/\/Case 2\n12.\t   color &#91;p&#91;z]] \u2190 BLACK           \/\/Case 3\n13.\t   color &#91;p&#91;z]] \u2190 RED             \/\/Case 3\n14.\t   RIGHT_ROTATE(T,p&#91;p&#91;z]])        \/\/Case 3\n15.\t else (same as then clause with \u201cright\u201d &amp; \u201cleft\u201d exchange)\n16. color &#91;root&#91;T]] \u2190  BLACK \n<\/code><\/pre>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13.png\"><img decoding=\"async\" width=\"1024\" height=\"431\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13-1024x431.png\" alt=\"Binary search algorithm\" class=\"wp-image-32453\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13-1024x431.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13-300x126.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13-768x323.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13-696x293.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13-1068x449.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13-998x420.png 998w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13-150x63.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-13.png 1462w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14.png\"><img decoding=\"async\" width=\"1024\" height=\"625\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-1024x625.png\" alt=\"Binary search algorithm\" class=\"wp-image-32454\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-1024x625.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-300x183.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-768x468.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-1536x937.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-696x425.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-1068x651.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-689x420.png 689w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14-150x92.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-14.png 1800w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n<figure class=\"wp-block-image size-large td-caption-align-https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15.png zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15.png\"><img decoding=\"async\" width=\"1024\" height=\"611\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-1024x611.png\" alt=\"Binary search algorithm\" class=\"wp-image-32455\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-1024x611.png 1024w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-300x179.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-768x458.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-1536x916.png 1536w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-696x415.png 696w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-1068x637.png 1068w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-704x420.png 704w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15-150x89.png 150w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2021\/04\/image-15.png 1904w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-binary-tree-and-binary-search-tree\"><strong>Difference between Binary Tree and Binary Search Tree<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Binary Tree<\/strong><\/td><td><strong>Binary Search Tree<\/strong><\/td><\/tr><tr><td>A non-linear data structure having at most two child nodes.<\/td><td>A node based binary tree where the further subtrees are also binary search tree<\/td><\/tr><tr><td>It is unordered.<\/td><td>It is ordered.<\/td><\/tr><tr><td>Slower in the process of searching, insertion and deletion.<\/td><td>Faster in the process of searching, insertion and deletion.<\/td><\/tr><tr><td>No ordering is there in terms of how the nodes will be arranged.<\/td><td>The elements of the left subtree has values lesser than that of the right sub tree.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This brings us to the end of the blog on Binary Search Algorithm. We hope that you enjoyed it and were able to learn the concept of Binary Search Algorithm well. If you wish to upskill, you can check out <a rel=\"noreferrer noopener\" aria-label=\"Great Learning Academy's Free online courses (opens in a new tab)\" href=\"https:\/\/www.mygreatlearning.com\/academy\" target=\"_blank\">Great Learning Academy's Free online courses<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Binary Search? Binary Search Algorithm is one of the widely used searching techniques. It can be used to sort arrays. This searching technique follows the divide and conquer strategy. The search space always reduces to half in every iteration. Binary Search Algorithm is a very efficient technique for searching but it needs some [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":22968,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[],"content_type":[],"class_list":["post-22598","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Binary Search Algorithm | What is Binary Search? - Great Learning<\/title>\n<meta name=\"description\" content=\"Binary Search Algorithm is one of the searching techniques. It can be used to sort arrays. Learn more about it in detail with the help of this blog.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Binary Search Algorithm | What is Binary Search?\" \/>\n<meta property=\"og:description\" content=\"Binary Search Algorithm is one of the searching techniques. It can be used to sort arrays. Learn more about it in detail with the help of this blog.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-11-10T05:11:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T05:54:36+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"Binary Search Algorithm | What is Binary Search?\",\"datePublished\":\"2020-11-10T05:11:24+00:00\",\"dateModified\":\"2024-09-03T05:54:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/\"},\"wordCount\":2230,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/November-5_blog-featured-image-binary-search-1.png\",\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/\",\"name\":\"Binary Search Algorithm | What is Binary Search? - Great Learning\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/November-5_blog-featured-image-binary-search-1.png\",\"datePublished\":\"2020-11-10T05:11:24+00:00\",\"dateModified\":\"2024-09-03T05:54:36+00:00\",\"description\":\"Binary Search Algorithm is one of the searching techniques. It can be used to sort arrays. Learn more about it in detail with the help of this blog.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/November-5_blog-featured-image-binary-search-1.png\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/11\\\/November-5_blog-featured-image-binary-search-1.png\",\"width\":1200,\"height\":675,\"caption\":\"GL_BinarySearch\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/binary-search-algorithm\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Binary Search Algorithm | What is Binary Search?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Binary Search Algorithm | What is Binary Search? - Great Learning","description":"Binary Search Algorithm is one of the searching techniques. It can be used to sort arrays. Learn more about it in detail with the help of this blog.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/","og_locale":"en_US","og_type":"article","og_title":"Binary Search Algorithm | What is Binary Search?","og_description":"Binary Search Algorithm is one of the searching techniques. It can be used to sort arrays. Learn more about it in detail with the help of this blog.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2020-11-10T05:11:24+00:00","article_modified_time":"2024-09-03T05:54:36+00:00","og_image":[{"width":1200,"height":675,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png","type":"image\/png"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"Binary Search Algorithm | What is Binary Search?","datePublished":"2020-11-10T05:11:24+00:00","dateModified":"2024-09-03T05:54:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/"},"wordCount":2230,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png","articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/","url":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/","name":"Binary Search Algorithm | What is Binary Search? - Great Learning","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png","datePublished":"2020-11-10T05:11:24+00:00","dateModified":"2024-09-03T05:54:36+00:00","description":"Binary Search Algorithm is one of the searching techniques. It can be used to sort arrays. Learn more about it in detail with the help of this blog.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png","width":1200,"height":675,"caption":"GL_BinarySearch"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/binary-search-algorithm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"Binary Search Algorithm | What is Binary Search?"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png",1200,675,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1-150x150.png",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1-300x169.png",300,169,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1-768x432.png",768,432,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1-1024x576.png",1024,576,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png",1200,675,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png",1200,675,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png",640,360,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png",96,54,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/11\/November-5_blog-featured-image-binary-search-1.png",150,84,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"What is Binary Search? Binary Search Algorithm is one of the widely used searching techniques. It can be used to sort arrays. This searching technique follows the divide and conquer strategy. The search space always reduces to half in every iteration. Binary Search Algorithm is a very efficient technique for searching but it needs some&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/22598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=22598"}],"version-history":[{"count":34,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/22598\/revisions"}],"predecessor-version":[{"id":98460,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/22598\/revisions\/98460"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/22968"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=22598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=22598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=22598"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=22598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}