{"id":9925,"date":"2021-07-09T11:03:01","date_gmt":"2021-07-09T05:33:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=9925"},"modified":"2021-11-22T18:50:10","modified_gmt":"2021-11-22T13:20:10","slug":"search-implementations-linear-and-binary","status":"publish","type":"post","link":"https:\/\/python-programs.com\/search-implementations-linear-and-binary\/","title":{"rendered":"Search implementations: Linear and Binary"},"content":{"rendered":"
Prerequisites<\/strong><\/p>\n To learn about Linear and Binary search, you’ll need to have a good understanding of:<\/p>\n Introduction<\/strong><\/p>\n Often we will have to find an element from a given data structure like lists, linked lists or binary trees. An efficient searching technique saves a great amount of time and improves performance. In this tutorial, we are going to see two very commonly used searching algorithms.<\/p>\n So if you were to search \u201cHarry Potter\u201d, from a shelf in a poorly lit room how would you go about it? You would start at one end, take one book at a time and check if it’s Harry Potter or not. You will use a brute force methodology which checks every book until the Harry Potter is found or the end of the shelf is reached. Best case scenario would be when Harry Potter is the first book and worst case would be the book not in there at all. Either way, you can know this only by checking each and every book. This is exactly what Linear Search is.<\/p>\n What if you were to search for Harry Potter from a shelf of books which are ordered alphabetically? Would you start searching from the start? Definitely not! You would start somewhere near the middle and check the first letter of the books and then go back or forward from there to find the ‘H’ titles. This means that you won\u2019t be looking at all the books, thus saving time. You also won’t need to go through the entire shelf to know whether the book is there or not. At a given point you are eliminating many books which you will never look at. Binary Search is similar to this.<\/p>\n NOTE:<\/b>\u00a0Linear Search can be done on both sorted and unsorted items but Binary Search can\u00a0only<\/em>\u00a0be done on a sorted set of items.<\/p>\n Now that you know what Linear and Binary Search methodologies are, let us look at how these searches would work on a list of numbers.<\/p>\n Given list A = [6,3,9,0,5,8,2] find if 0 is present in this list or not.<\/p>\n Code<\/p>\n print(linearSearch([6,3,9,5,8,2],0))<\/p>\n The idea is to keep comparing the element with the middle value. This way with each search we eliminate one half of the list.<\/p>\n NOTE:<\/b>\u00a0The list continues to get divided into two and the middle element gets compared until the element is found or no more elements are left to compare with.<\/p>\n Code<\/p>\n Given list B = [2,5,7,8,9,11,14,16] find if 14 is present in this list or not.<\/p>\n NOTE:<\/b>\u00a0To find the mid element, \u201c(first+last)\/\/2\u201d is used instead of \u201c(first+last)\/2\u201d. This gives us whole numbers especially when the length of the list is odd. Try 9\/2 and 9\/\/2 in your Python IDLE to get a better understanding.<\/p>\n Try out\u00a0this\u00a0animation for a better understanding of both these search algorithms. Try to find the first element of the array. Which is faster?<\/p>\n Conclusion<\/strong><\/p>\n From the above explanation, it must be clear that Binary Search is consistently faster than Linear Search. If you are familiar with o(n) notation, Linear Search is o(n) and Binary Search is log(n)<\/p>\n You can perform Linear Searches on a sorted list as well with a small optimization. You can stop the search once the number to be found exceeds the element being compared to. For instance, you want to search for 12 from the list [2,4,5,13,16,19], once you reach 13 you can stop because there is no way that 12 is going to come after 13 in a sorted list.<\/p>\n In this tutorial, we have discussed only the iterative method of Binary Search. Try to implement the recursive approach on your own. \u00a0Also, learn about the uses of these searches and when to apply them. That\u2019s all for this tutorial. Happy Pythoning!<\/p>\n","protected":false},"excerpt":{"rendered":" Prerequisites To learn about Linear and Binary search, you’ll need to have a good understanding of: Python 3 Basic Python data structure concepts – lists Introduction Often we will have to find an element from a given data structure like lists, linked lists or binary trees. An efficient searching technique saves a great amount of …<\/p>\n\n
Linear Search<\/h2>\n
Binary Search<\/h2>\n
Implementation<\/h3>\n
Linear Search<\/h4>\n
Algorithm<\/h4>\n
\n
\n
def linearSearch(ls,data):\r\n\r\n for item in ls:\r\n if item == data:\r\n return True\r\n return False<\/pre>\n
Binary Search<\/h4>\n
Algorithm<\/h4>\n
\n
\n
def binarySearch(ls,data):\r\n first = 0\r\n last = len(ls)-1\r\n done = False \r\n\r\n while first<=last and not done:\r\n mid = (first+last)\/\/2\r\n if ls[mid] == data:\r\n done = True\r\n else:\r\n if ls[mid] > data:\r\n last = last-1\r\n else:\r\n first = first+1\r\n return done \r\n\r\nprint(binarySearch([2,5,7,8,9,11,14,16],4))<\/pre>\n
\n\n
\n Round<\/td>\n First<\/td>\n Last<\/td>\n Mid<\/td>\n ls[mid]<\/td>\n Result<\/td>\n<\/tr>\n \n 1<\/td>\n 0<\/td>\n 7<\/td>\n 3<\/td>\n 8<\/td>\n 8<14<\/td>\n<\/tr>\n \n 2<\/td>\n 4<\/td>\n 7<\/td>\n 5<\/td>\n 11<\/td>\n 11<14<\/td>\n<\/tr>\n \n 3<\/td>\n 6<\/td>\n 7<\/td>\n 6<\/td>\n 14<\/td>\n 14=14<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n