{"componentChunkName":"component---src-templates-blog-post-jsx","path":"/blogs/binarySearch","result":{"data":{"blog":{"frontmatter":{"title":"BINARY SEARCH","thumbnail":"blog57","date":"April 20, 2021","dsaCppCodeFile":"https://drive.google.com/file/d/1EOeLgDQF6oafjKwsuaAV8UdM5gvGyFb6/view?usp=sharing"},"excerpt":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Binary Search Algorithm is a method for finding an element\n                  within a given data structure given that the list is sorted.\n                  It is more efficient than linear search because this algorithm\n                  always targets the mid element of the data structure and\n                  divide the search space into half untill the match is found.\n                </p>\n                <p>Let&apos;s look at the working of this algorithm.</p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Consider an array of n elements which are sorted in ascending\n                  order.\n                </li>\n                <li>\n                  Create variables namely: low = 0, high = n-1, mid and pos =\n                  -1(Index of the element, if founded).\n                </li>\n                <li>\n                  Loop if low &lt; high\n                  <ol class=\"pl-4\">\n                    <li>Find the mid value, mid = (low + high) / 2.</li>\n                    <li>\n                      Check the array value at mid index with the element to be\n                      searched.\n                    </li>\n                    <li>\n                      If it matches then assign the mid+1 value to pos variable\n                      and break the loop.\n                    </li>\n                    <li>\n                      Else if array value at mid index is lesser than the\n                      element to be searched, then assign low = mid + 1. This\n                      will make the algorithm to search in second half(every\n                      time) there by reducing the time complexity.\n                    </li>\n                    <li>\n                      Else, assign high = mid - 1(eventually array value at mid\n                      index will be greater than the element to be searched).\n                      This will make the algorithm to search in first half(every\n                      time) there by reducing the time complexity.\n                    </li>\n                  </ol>\n                </li>\n                <li>\n                  After looping, if pos value is still -1 then it means that the\n                  element is not found in the given array.\n                </li>\n                <li>\n                  If it&apos;s not -1 then element is found the position whose value\n                  is pos.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Time Complexity</h4>\n              <div class=\"m-2\">\n                <p class=\"text-muted\">\n                  <strong>PS: We give random inputs because the no. of input will\n                    generally be 100 or 1000 to find the time complexity and\n                    manually giving so many inputs is cumbersome work.</strong>\n                </p>\n                <p>\n                  The basic operation in this algorithm is the comparision\n                  between each array element and the element to be searched(step\n                  4.2 in the above working procedure). So increment the count\n                  variable above this if condition to get the count of no. of\n                  times the basic operation has been performed.\n                </p>\n                <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 73.64864864864865%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABv0lEQVQ4y51UCXKDMAz0/19YkkIgF8Tc4cgFqLtKYELbpDPVjLAN1mp1YYZhkHfS973keS51XUtZlqq3rnt53/BB0Mvlono+nyfluWka8TxPAj+Q1WolrutK27bqqAPw9XqVM22x8p0hGDexjSWOYymKQrIskzRNdZ9iT1AyI0t+cxxHfIBztVEkOexsEEiD+8qQlzebjRyPR/lLuqEXmyRS4q4F0Ol0EjykXa+lgkNDJt3tJhE8EXhMwQ/le+YOqRCusNEVoYK6NHBQIxLD3DBX+/1ew5kBcv+k8CgDjAcYToozgcmOEZoAsTPJBJwxvG/uSqkqDe27jD3CPDPHhkAUhj4xpDKfdDCuBHx28tCx7QhW4Y7xXE8PyrB4MHyENQFMdIb5+RHNjKHz4WgODyhKhgZWryj/8Ko4v+iMYYIWoCRJiokoNMQeFfw3oPbW7Sqb3U5K5vPNWL0TFlZD5pRQchQlwljlnA7sOTWcYa4s1uFwEEbDSMZv1trpDr+zS8xIOQPIDgzZRmEY6vyy2X3fV4PtdgujSO/sEA2L6LqfCrRcLiWMQp1z81ypV7IC+HKxkAUM1xgx/jh+E+J8ARc1i+kg5EoCAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Binary Search\" title=\"Binary Search\" src=\"/static/b83c89b22927196838085c0f487fe149/fcda8/graph.png\" srcset=\"/static/b83c89b22927196838085c0f487fe149/12f09/graph.png 148w,\n/static/b83c89b22927196838085c0f487fe149/e4a3f/graph.png 295w,\n/static/b83c89b22927196838085c0f487fe149/fcda8/graph.png 590w,\n/static/b83c89b22927196838085c0f487fe149/efc66/graph.png 885w,\n/static/b83c89b22927196838085c0f487fe149/6bfd0/graph.png 1011w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n    </span>\n                <p class=\"lead\"><strong>Time Complexity: O(logn)</strong></p>\n              </div>\n              <div class=\"my-2 p-2\">\n                  <strong>Similar posts:</strong>\n                  <a href=\"/blogs/linearSearch\">Linear Search</a>\n              </div>\n            </div>\n","html":"<div class=\"my-2 p-2\">\n              <h4>Introduction</h4>\n              <div class=\"m-2\">\n                <p>\n                  Binary Search Algorithm is a method for finding an element\n                  within a given data structure given that the list is sorted.\n                  It is more efficient than linear search because this algorithm\n                  always targets the mid element of the data structure and\n                  divide the search space into half untill the match is found.\n                </p>\n                <p>Let&apos;s look at the working of this algorithm.</p>\n              </div>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Working Procedure</h4>\n              <p class=\"text-muted\">\n                Do refer the code available the end of this section to\n                understand the following theory.\n              </p>\n              <ol class=\"pl-4\">\n                <li>\n                  Consider an array of n elements which are sorted in ascending\n                  order.\n                </li>\n                <li>\n                  Create variables namely: low = 0, high = n-1, mid and pos =\n                  -1(Index of the element, if founded).\n                </li>\n                <li>\n                  Loop if low &lt; high\n                  <ol class=\"pl-4\">\n                    <li>Find the mid value, mid = (low + high) / 2.</li>\n                    <li>\n                      Check the array value at mid index with the element to be\n                      searched.\n                    </li>\n                    <li>\n                      If it matches then assign the mid+1 value to pos variable\n                      and break the loop.\n                    </li>\n                    <li>\n                      Else if array value at mid index is lesser than the\n                      element to be searched, then assign low = mid + 1. This\n                      will make the algorithm to search in second half(every\n                      time) there by reducing the time complexity.\n                    </li>\n                    <li>\n                      Else, assign high = mid - 1(eventually array value at mid\n                      index will be greater than the element to be searched).\n                      This will make the algorithm to search in first half(every\n                      time) there by reducing the time complexity.\n                    </li>\n                  </ol>\n                </li>\n                <li>\n                  After looping, if pos value is still -1 then it means that the\n                  element is not found in the given array.\n                </li>\n                <li>\n                  If it&apos;s not -1 then element is found the position whose value\n                  is pos.\n                </li>\n              </ol>\n            </div>\n            <div class=\"my-2 p-2\">\n              <h4>Time Complexity</h4>\n              <div class=\"m-2\">\n                <p class=\"text-muted\">\n                  <strong>PS: We give random inputs because the no. of input will\n                    generally be 100 or 1000 to find the time complexity and\n                    manually giving so many inputs is cumbersome work.</strong>\n                </p>\n                <p>\n                  The basic operation in this algorithm is the comparision\n                  between each array element and the element to be searched(step\n                  4.2 in the above working procedure). So increment the count\n                  variable above this if condition to get the count of no. of\n                  times the basic operation has been performed.\n                </p>\n                <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \">\n      <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 73.64864864864865%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAPCAYAAADkmO9VAAAACXBIWXMAABJ0AAASdAHeZh94AAABv0lEQVQ4y51UCXKDMAz0/19YkkIgF8Tc4cgFqLtKYELbpDPVjLAN1mp1YYZhkHfS973keS51XUtZlqq3rnt53/BB0Mvlono+nyfluWka8TxPAj+Q1WolrutK27bqqAPw9XqVM22x8p0hGDexjSWOYymKQrIskzRNdZ9iT1AyI0t+cxxHfIBztVEkOexsEEiD+8qQlzebjRyPR/lLuqEXmyRS4q4F0Ol0EjykXa+lgkNDJt3tJhE8EXhMwQ/le+YOqRCusNEVoYK6NHBQIxLD3DBX+/1ew5kBcv+k8CgDjAcYToozgcmOEZoAsTPJBJwxvG/uSqkqDe27jD3CPDPHhkAUhj4xpDKfdDCuBHx28tCx7QhW4Y7xXE8PyrB4MHyENQFMdIb5+RHNjKHz4WgODyhKhgZWryj/8Ko4v+iMYYIWoCRJiokoNMQeFfw3oPbW7Sqb3U5K5vPNWL0TFlZD5pRQchQlwljlnA7sOTWcYa4s1uFwEEbDSMZv1trpDr+zS8xIOQPIDgzZRmEY6vyy2X3fV4PtdgujSO/sEA2L6LqfCrRcLiWMQp1z81ypV7IC+HKxkAUM1xgx/jh+E+J8ARc1i+kg5EoCAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"Binary Search\" title=\"Binary Search\" src=\"/static/b83c89b22927196838085c0f487fe149/fcda8/graph.png\" srcset=\"/static/b83c89b22927196838085c0f487fe149/12f09/graph.png 148w,\n/static/b83c89b22927196838085c0f487fe149/e4a3f/graph.png 295w,\n/static/b83c89b22927196838085c0f487fe149/fcda8/graph.png 590w,\n/static/b83c89b22927196838085c0f487fe149/efc66/graph.png 885w,\n/static/b83c89b22927196838085c0f487fe149/6bfd0/graph.png 1011w\" sizes=\"(max-width: 590px) 100vw, 590px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n    </span>\n                <p class=\"lead\"><strong>Time Complexity: O(logn)</strong></p>\n              </div>\n              <div class=\"my-2 p-2\">\n                  <strong>Similar posts:</strong>\n                  <a href=\"/blogs/linearSearch\">Linear Search</a>\n              </div>\n            </div>"},"thumbnail":{"childImageSharp":{"fluid":{"tracedSVG":"data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='400'%20height='225'%20viewBox='0%200%20400%20225'%20preserveAspectRatio='none'%3e%3cpath%20d='M42%2095l-1%208v9c0%201%200%202%205%202h5v-4l1-4%201%204c2%203%203%204%205%204s2%200%202-7l1-10c0-2%200-2-2-2-3%200-3%200-3%203v4l-2-4c-1-2-2-3-4-3l-5-1-3%201m27%200c-2%200-8%2015-8%2018h6c1-3%204-3%204-1s1%202%206%202%205%200%205-3c0-4%202-4%204%200%201%203%201%203%203%202s2-1%201-4v-4l1-6v-3H79l-1%208c-1%209-1%209-3-1-1-7-3-9-6-8m65%200c-5%200-5%200-5%203v3l-2-3c-1-2-2-3-4-3-7%200-9%207-4%2011%204%203%204%204%200%204-4-1-5%202-1%203%204%202%208%200%209-2s1-2%201%200c0%203%200%203%206%203s7-1%207-3%200-2-4-2-5-1-2-2l3-1%202-2c0-2-1-2-3-2-6%200-5-2%201-2%204-1%204-1%204-3-1-3-1-3-8-2m16%200l-3%202-5%2016c0%202%205%201%206%200%201-3%204-3%204-1s1%202%206%202%205%200%205-3c0-4%202-4%204%200%201%203%201%203%203%202s2-1%201-4V96l-6-1-5%201-1%208c-1%209-1%209-3-1-1-7-3-9-6-8m28%201c-7%205-6%2018%202%2018%203%200%208-5%206-7-1-1-2-1-3%201l-3%201c-2-1-1-7%200-9%202-1%202-1%202%201%200%201%201%202%203%202%203%200%203-7-1-8-3-1-3-1-6%201m12-1l-2%2010v9h2c2%200%203-1%203-2v-4l2-2c2%200%202%201%202%204%200%204%200%204%203%204%202%200%202%200%202-7a1840%201840%200%2001-1-13c-3%200-4%202-4%205l-1%203c-2%200-2-1-2-4s0-4-2-4l-2%201M28%2096l-2%209v9h5l6-1c2-1%202-3%202-8l1-4c1-4-1-6-7-6l-5%201m65%200l2%207c1%204%202%207%201%209%200%202%200%202%202%202%203%200%203%200%203-3l1-4%203-7%202-5h-3l-3%203-2%204-1-4c-1-3-4-4-5-2m206%2010l-1%208v7h9c8%200%209%201%209%203s0%202-2%202c-5-1-16%2013-21%2026l-3%2020%206-7c4-5%206-7%2010-8%206-3%206-2%206%201%200%202%200%202%201%201h3l-2-5-2-7-1-3v-3c3-2%2013-2%2013%200l-1%201h-2c0%202%203%202%204%201h1l-1%202-1%203-2%207-2%204h3c2%200%202-1%201-3%200-2%200-2%207%200%204%201%205%202%207%205l3%204%201%203%202%206%202%204%201%204c2%207-1%2010-9%208h-4v-24h-15v-3l-1-3-1%203v3h-17v7l-1%208c-2%202-2%203-1%203%202%200%203%204%202%206h-15c-2-3-2-5%201-8%204-6%202-7-2-2-3%203-4%205-4%207%200%203%200%203-9%204-9%200-9%200-9%202s1%203%205%203h5l-1%204v3h97l1-4c1-2%202-3%205-3%205-1%205-4%200-5h-3v-14c0-14-1-21-4-21l-2-2c-1-2-1-2-1%200%200%203-8%203-8-1%200-2%200-2%202-2%202%201%203%200%201-1-2-2-1-4%203-12%206-11%205-17-1-20l-3-2-1-1-1-4-1-5c-1-1-1%201-1%204v5l-3-1c-3-1-22%200-24%201-1%201-1-1-1-4v-5l-14-1-13%201m2%207v7h22v-3l1-3v-1l-1-3v-3h-22v6m-31%2028l1%209h12v-18h-13v9m3%2023l-1%205c0%203%200%204-1%203%200-4-2-3-3%200l2%204%202%203%203%201c2%200%202%200%202-3%200-2%200-3%202-3s2-1%202-4l-1-4-1%203c0%203-2%202-2-2%200-3-1-4-4-3'%20fill='%23d3d3d3'%20fill-rule='evenodd'/%3e%3c/svg%3e","aspectRatio":1.7699115044247788,"src":"/static/82a6dcabec33b718c6eb802d5e386545/ee604/blog57.png","srcSet":"/static/82a6dcabec33b718c6eb802d5e386545/69585/blog57.png 200w,\n/static/82a6dcabec33b718c6eb802d5e386545/497c6/blog57.png 400w,\n/static/82a6dcabec33b718c6eb802d5e386545/ee604/blog57.png 800w,\n/static/82a6dcabec33b718c6eb802d5e386545/f3583/blog57.png 1200w,\n/static/82a6dcabec33b718c6eb802d5e386545/e4d72/blog57.png 1280w","sizes":"(max-width: 800px) 100vw, 800px"}}}},"pageContext":{"blog":"binarySearch","thumbnail":"thumbnails/blog57.png"}},"staticQueryHashes":["2987289216","63159454"]}