{"id":25016,"date":"2017-10-15T14:00:00","date_gmt":"2017-10-15T08:30:00","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25016"},"modified":"2017-10-15T14:00:00","modified_gmt":"2017-10-15T08:30:00","slug":"interpolation-search","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/interpolation-search\/","title":{"rendered":"Interpolation Search"},"content":{"rendered":"<p>Given a sorted array of n uniformly distributed values arr[], write a function to search for a particular element x in the array.<\/p>\n<p>Linear Search finds the element in O(n) time, Jump Search takes O(\u221a n) time and Binary Search take O(Log n) time.<\/p>\n<p>The Interpolation Search is an improvement over Binary Search for instances, where the values in a sorted array are uniformly distributed. Binary Search always goes to middle element to check. On the other hand interpolation search may go to different locations according the value of key being searched. For example if the value of key is closer to the last element, interpolation search is likely to start search toward the end side.<\/p>\n<p>To find the position to be searched, it uses following formula.<\/p>\n<p>\/\/ The idea of formula is to return higher value of <strong>pos<\/strong><\/p>\n<p>\/\/ when element to be searched is closer to <strong>arr[hi]<\/strong>. And<\/p>\n<p>\/\/ smaller value when closer to <strong>arr[lo]<\/strong><\/p>\n<p>pos = lo + [ (x-arr[lo])*(hi-lo) \/ (arr[hi]-arr[Lo]) ]\n<p>arr[] ==> Array where elements need to be searched<\/p>\n<p>x ==> Element to be searched<\/p>\n<p>lo ==> Starting index in arr[]\n<p>hi ==> Ending index in arr[]\n[ad type=\u201dbanner\u201d]\n<p><strong>Algorithm<\/strong><br \/>\nRest of the Interpolation algorithm is same except the above partition logic.<\/p>\n<p><strong>Step1:<\/strong> In a loop, calculate the value of \u201cpos\u201d using the probe position formula.<br \/>\n<strong>Step2:<\/strong> If it is a match, return the index of the item, and exit.<br \/>\n<strong>Step3:<\/strong> If the item is less than arr[pos], calculate the probe position of the left sub-array. Otherwise calculate the same in the right sub-array.<br \/>\n<strong>Step4:<\/strong> Repeat until a match is found or the sub-array reduces to zero.<\/p>\n<p>Below is C implementation of algorithm.<\/p>\n<table style=\"height: 319px;\" border=\"0\" width=\"405\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\">\n<p><code class=\"c comments\"><\/code><\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%2F%2F%20C%20program%20to%20implement%20interpolation%20search%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20If%20x%20is%20present%20in%20arr%5B0..n-1%5D%2C%20then%20returns%0A%2F%2F%20index%20of%20it%2C%20else%20returns%20-1.%0Aint%20interpolationSearch(int%20arr%5B%5D%2C%20int%20n%2C%20int%20x)%0A%7B%0A%20%20%20%20%2F%2F%20Find%20indexes%20of%20two%20corners%0A%20%20%20%20int%20lo%20%3D%200%2C%20hi%20%3D%20(n%20-%201)%3B%0A%20%0A%20%20%20%20%2F%2F%20Since%20array%20is%20sorted%2C%20an%20element%20present%0A%20%20%20%20%2F%2F%20in%20array%20must%20be%20in%20range%20defined%20by%20corner%0A%20%20%20%20while%20(lo%20%3C%3D%20hi%20%26%26%20x%20%3E%3D%20arr%5Blo%5D%20%26%26%20x%20%3C%3D%20arr%5Bhi%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Probing%20the%20position%20with%20keeping%0A%20%20%20%20%20%20%20%20%2F%2F%20uniform%20distribution%20in%20mind.%0A%20%20%20%20%20%20%20%20int%20pos%20%3D%20lo%20%2B%20(((double)(hi-lo)%20%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20(arr%5Bhi%5D-arr%5Blo%5D))*(x%20-%20arr%5Blo%5D))%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Condition%20of%20target%20found%0A%20%20%20%20%20%20%20%20if%20(arr%5Bpos%5D%20%3D%3D%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20pos%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20larger%2C%20x%20is%20in%20upper%20part%0A%20%20%20%20%20%20%20%20if%20(arr%5Bpos%5D%20%3C%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20lo%20%3D%20pos%20%2B%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20smaller%2C%20x%20is%20in%20lower%20part%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20hi%20%3D%20pos%20-%201%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20-1%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20Code%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Array%20of%20items%20on%20which%20search%20will%0A%20%20%20%20%2F%2F%20be%20conducted.%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%20%7B10%2C%2012%2C%2013%2C%2016%2C%2018%2C%2019%2C%2020%2C%2021%2C%2022%2C%2023%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2024%2C%2033%2C%2035%2C%2042%2C%2047%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%0A%20%20%20%20int%20x%20%3D%2018%3B%20%2F%2F%20Element%20to%20be%20searched%0A%20%20%20%20int%20index%20%3D%20interpolationSearch(arr%2C%20n%2C%20x)%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20element%20was%20found%0A%20%20%20%20if%20(index%20!%3D%20-1)%0A%20%20%20%20%20%20%20%20printf(%22Element%20found%20at%20index%20%25d%22%2C%20index)%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20printf(%22Element%20not%20found.%22)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dc\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Output :<\/p>\n<p>Element found at index 4<\/p>\n<p>Time Complexity : If elements are uniformly distributed, then <strong>O (log log n))<\/strong>. In worst case it can take upto O(n).<br \/>\nAuxiliary Space : O(1)<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>Interpolation Search &#8211; searching and sorting algorithm- The Interpolation Search is an improvement over Binary Search for instances, where the values .<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,71670],"tags":[70504,70518,70744,70054,1958,70728,2529,70468,70751,70050,70456,70727,70481,70479,70478,70502,70475,70052,70264,70270,70263,70268,70505,70753,70513,70491,70516,70461,70465,70498,70459,70499,70495,70464,70477,70457,70755,70497,70757,70736,70486,70488,70509,70472,70454,70048,70730,70746,70729,70493,70752,70723,70494,70758,70740,70726,70514,70756,70453,70487,70458,70483,70510,70515,70750,70511,70737,70482,70760,70490,70724,70473,70476,70471,70467,70734,70463,70738,70484,70470,70492,70507,70731,70271,70742,70749,70743,70735,70733,70732,70747,70754,70496,70745,70761,70049,70262,70046,70056,70748,70725,70722,70026,70759,70741,70739],"class_list":["post-25016","post","type-post","status-publish","format-standard","hentry","category-coding","category-searching-and-sorting","tag-abstract-data-type","tag-adjacent","tag-aggregation","tag-algorithm","tag-alias","tag-ancestors","tag-and","tag-ansi","tag-arp","tag-array","tag-artificial-intelligence","tag-assert","tag-assignment-problem","tag-automata","tag-avl-tree","tag-backtrack","tag-bdd","tag-binary-search","tag-binary-search-algorithm","tag-binary-search-in-c","tag-binary-search-tree","tag-binary-tree","tag-bipartite-graph","tag-bloom-filter","tag-boolean","tag-boolean-expression","tag-branch-and-bound","tag-bubble-sort","tag-bucket-sort","tag-centroid","tag-certificate","tag-chinese-remainder-theorem","tag-circular-queue","tag-cluster","tag-codeword","tag-coding","tag-concurrency","tag-counting-sort","tag-critical-section","tag-ctl","tag-cube-root","tag-cyclic-redundancy-check","tag-dag","tag-data-compression","tag-data-mining","tag-data-structures","tag-database-normalization","tag-david-filo","tag-decision-tree","tag-depth","tag-deranged","tag-descendants","tag-determinism","tag-diameter","tag-dijkstra","tag-dijkstras-algorithm","tag-disjoint-set","tag-divide-and-conquer","tag-domain","tag-doubly-linked-list","tag-dynamic","tag-dynamic-programming","tag-euclidean-algorithm","tag-euclidean-distance","tag-exponential","tag-factorial","tag-fast-fourier-transform","tag-fibonacci-numbers","tag-first-come-first-serve","tag-fsm","tag-function","tag-gamma-function","tag-genetic-algorithm","tag-google-code","tag-google-project","tag-google-summer-of-code","tag-graph","tag-graph-data-structure","tag-gray-code","tag-gwt","tag-hash-function","tag-hash-table","tag-heap","tag-heap-sort","tag-intellij","tag-interpolation","tag-interpolation-formula","tag-java-api","tag-java-hibernate","tag-java-plugin","tag-java-thread","tag-kruskals-algorithm","tag-linear-search","tag-linear-search-in-c","tag-linear-search-program-in-c","tag-linked-list","tag-merge-sort","tag-quicksort","tag-recursion","tag-red-black-tree","tag-servlet","tag-struts","tag-time-complexity-of-binary-search","tag-topological-sort","tag-tree-traversal","tag-turing-machine"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25016","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=25016"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25016\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}