{"id":6734,"date":"2021-09-30T15:30:38","date_gmt":"2021-09-30T10:00:38","guid":{"rendered":"https:\/\/python-programs.com\/?p=6734"},"modified":"2021-11-22T18:39:29","modified_gmt":"2021-11-22T13:09:29","slug":"binary-search-algorithm-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/binary-search-algorithm-in-python\/","title":{"rendered":"Binary Search Algorithm in Python"},"content":{"rendered":"
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners<\/a> tutorial.<\/p>\n Binary Search:<\/strong><\/p>\n A binary search is an algorithm for finding a specific element in a list. Assume we have a list of a thousand elements and we need to find the index location of a specific element. Using the binary search algorithm, we can quickly determine the index location of an element.<\/p>\n There are several search algorithms, but the binary search is the most widely used.<\/p>\n To use the binary search algorithm, the elements in the list must be sorted. If the elements are not already sorted, sort them first.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Below is the implementation of linear search<\/strong>:<\/p>\n Output:<\/strong><\/p>\n Consider a basic searching algorithm such as linear search, in which we must go through each element one by one before we find what we’re looking for. This means that for larger input sizes, the time it takes to find an element grows in lockstep with the input size. Its time complexity is O measurably (n).<\/p>\n The term “time complexity” refers to the measurement of how quick or efficient an algorithm is. The time complexity of Binary Search is \u201cO(log2n),\u201d which means that if we double the size of the input list, the algorithm can only perform one additional iteration.<\/p>\n In the same way, if the input size is multiplied by a thousand, the loop would only need to run 10 times more.<\/p>\n Remember that half of the list is removed with each iteration, so eliminating the entire list won’t take long. Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial. Binary Search: A binary search is an algorithm for finding a specific element in a list. Assume we have a list of a thousand elements and we need to find the index location of …<\/p>\ngiven_elements =[2, 7, 3, 4, 9, 15]\u00a0 \u00a0key= 9<\/pre>\n
Element 9 is found at index 4<\/pre>\n
Binary Search Algorithm in Python<\/h2>\n
\n
1)Algorithm<\/h3>\n
\n
2)Implementation<\/h3>\n
# function which return index if element is present else return -1\r\ndef binarySearch(given_list, key):\r\n lowindex = 0\r\n highindex = len(given_list) - 1\r\n midindex = 0\r\n # loop till lowindex is less than or equal to highindex\r\n while lowindex <= highindex:\r\n # Modify midindex with average of high index and lowindex\r\n midindex = (highindex + lowindex) \/\/ 2\r\n\r\n # If key is greater than mid element ignore left half side of list\r\n if given_list[midindex] < key:\r\n lowindex = midindex + 1\r\n\r\n # If key is less than mid element ignore right half side of list\r\n elif given_list[midindex] > key:\r\n highindex = midindex - 1\r\n\r\n # Else the element is present at mid index\r\n else:\r\n return midindex\r\n\r\n # if any value is not returned then element is not present in list\r\n return -1\r\n\r\n\r\n# given_list\r\ngiven_list = [2, 7, 3, 4, 9, 15]\r\n# given key\r\nkey = 9\r\n# sort the list\r\ngiven_list.sort()\r\n# passing the given_list and key to binarySearch function\r\nres = binarySearch(given_list, key)\r\n# if result is equal to -1 then element is not present in list\r\nif(res == -1):\r\n print(\"Given element(key) is not found in list\")\r\nelse:\r\n print(\"Element\", key, \"is found at index\", res)\r\n<\/pre>\n
Element 9 is found at index 4<\/pre>\n
3)Time Complexity<\/h3>\n
\nRelated Programs<\/strong>:<\/p>\n\n