{"id":24947,"date":"2017-10-15T13:58:56","date_gmt":"2017-10-15T08:28:56","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=24947"},"modified":"2017-10-15T13:58:56","modified_gmt":"2017-10-15T08:28:56","slug":"ternary-search","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/ternary-search\/","title":{"rendered":"TERNARY SEARCH"},"content":{"rendered":"<h1 id=\"why-is-binary-search-preferred-over-ternary-search\" class=\"entry-title\">Why is Binary Search preferred over Ternary Search?<\/h1>\n<p>The following is a simple recursive <strong>Binary Search<\/strong> function in C++ taken<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A recursive binary search function. It returns location of x in<br\/>\/\/ given array arr[l..r] is present, otherwise -1<br\/>int binarySearch(int arr[], int l, int r, int x)<br\/>{<br\/>   if (r &gt;= l)<br\/>   {<br\/>        int mid = l + (r - l)\/2;<br\/>  <br\/>        \/\/ If the element is present at the middle itself<br\/>        if (arr[mid] == x)  return mid;<br\/>  <br\/>        \/\/ If element is smaller than mid, then it can only be present<br\/>        \/\/ in left subarray<br\/>        if (arr[mid] &gt; x) return binarySearch(arr, l, mid-1, x);<br\/>  <br\/>        \/\/ Else the element can only be present in right subarray<br\/>        return binarySearch(arr, mid+1, r, x);<br\/>   }<br\/>  <br\/>   \/\/ We reach here when element is not present in array<br\/>   return -1;<br\/>} <\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p>The following is a simple recursive <strong>Ternary Search<\/strong> function in C++.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C++<\/span> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A recursive ternary search function. It returns location of x in<br\/>\/\/ given array arr[l..r] is present, otherwise -1<br\/>int ternarySearch(int arr[], int l, int r, int x)<br\/>{<br\/>   if (r &gt;= l)<br\/>   {<br\/>        int mid1 = l + (r - l)\/3;<br\/>        int mid2 = mid1 + (r - l)\/3;<br\/> <br\/>        \/\/ If x is present at the mid1<br\/>        if (arr[mid1] == x)  return mid1;<br\/> <br\/>        \/\/ If x is present at the mid2<br\/>        if (arr[mid2] == x)  return mid2;<br\/> <br\/>        \/\/ If x is present in left one-third<br\/>        if (arr[mid1] &gt; x) return ternarySearch(arr, l, mid1-1, x);<br\/> <br\/>        \/\/ If x is present in right one-third<br\/>        if (arr[mid2] &lt; x) return ternarySearch(arr, mid2+1, r, x);<br\/> <br\/>        \/\/ If x is present in middle one-third<br\/>        return ternarySearch(arr, mid1+1, mid2-1, x);<br\/>   }<br\/>   \/\/ We reach here when element is not present in array<br\/>   return -1;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Which of the above two does less comparisons in worst case?<\/strong><\/p>\n<p>From the first look, it seems the ternary search does less number of comparisons as it makes Log<sub>3<\/sub>n recursive calls, but binary search makes Log<sub>2<\/sub>n recursive calls. Let us take a closer look.<br \/>\nThe following is recursive formula for counting comparisons in worst case of Binary Search.<\/p>\n<p>T(n) = T(n\/2) + 2, T(1) = 1<\/p>\n<p>The following is recursive formula for counting comparisons in worst case of Ternary Search.<\/p>\n<p>T(n) = T(n\/3) + 4, T(1) = 1<\/p>\n<p>In binary search, there are 2Log<sub>2<\/sub>n + 1 comparisons in worst case. In ternary search, there are 4Log<sub>3<\/sub>n + 1 comparisons in worst case.<\/p>\n<p>Time Complexity for Binary search = 2clog<sub>2<\/sub>n + O(1)<\/p>\n<p>Time Complexity for Ternary search = 4clog<sub>3<\/sub>n + O(1)<\/p>\n<p>Therefore, the comparison of Ternary and Binary Searches boils down the comparison of expressions 2Log<sub>3<\/sub>n and Log<sub>2<\/sub>n . The value of 2Log<sub>3<\/sub>n can be written as (2 \/ Log<sub>2<\/sub>3) * Log<sub>2<\/sub>n . Since the value of (2 \/ Log<sub>2<\/sub>3) is more than one, Ternary Search does more comparisons than Binary Search in worst case.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>TERNARY SEARCH &#8211; searching and sorting algorithm &#8211; The ternary search does less number of comparisons as it makes Log3n recursive calls, but binary search.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82509,1,71670],"tags":[70276,70283,70664,70052,70264,70111,70669,7378,70660,70667,70656,70651,70665,70649,70658,70292,70272,70670,70099,70668,70671,70280,70097,70315,70308,70072,70652,70653,70666,70659,70654,70655,70650,70663,70657,70662,70661],"class_list":["post-24947","post","type-post","status-publish","format-standard","hentry","category-binary-search-tree-2","category-coding","category-searching-and-sorting","tag-algorithm-for-binary-search","tag-algorithm-of-binary-search","tag-balanced-search-tree","tag-binary-search","tag-binary-search-algorithm","tag-binary-search-topcoder","tag-binary-search-tree-applications","tag-c-string","tag-data-structure-tree","tag-data-structures-geeksforgeeks","tag-grams-search-engine","tag-java-test","tag-multiway-search-tree","tag-optimization","tag-radix-tree","tag-recursive-binary-search-algorithm","tag-search-algorithms","tag-search-function","tag-search-inc","tag-search-protect","tag-search-string","tag-search-tree","tag-searching-algorithms-in-c","tag-searching-algorithms-in-java","tag-searching-c","tag-searching-in-data-structure","tag-sorting-algorithms-in-c","tag-spelling-test","tag-string-sorting-in-c","tag-ternary","tag-ternary-search","tag-ternary-search-tree","tag-tree-data-structure","tag-tree-data-structure-in-c","tag-tree-java","tag-trees-inc","tag-trie-tree"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/24947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=24947"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/24947\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=24947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=24947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=24947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}