In this article we will see how we can make a PyQt5 application which will visualize the Binary search algorithm.
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
GUI implementation steps :
1. Create a list of label according to the given list of numbers
2. Set their text, border, color and geometry with respective gap from each other
3. Each label height should be proportional to the value of each number
4. Create a start and pause push button to start the searching and pause the searching
5. Create a result label to show the searching status
Algorithm implementation steps :
1. Create label list corresponding to the given numbers
2. Create variable for the first last and mid index and flag for searching.
3. Add action to the push button, their action should change the flag status i.e start action should make flag true and pause action should make flag false
4. Create timer object which calls a method after every specific time
5. Inside the timer method check for the flag is flag is true begin the binary search algorithm
6. Calculate mid value and check if mid value is equal then stop the search and make label green.
7. Show result as found else change the value of first and last search according to the mid value. Make the label grey color and continue the search.
8. If the index of first become greater then index of last stop the search and show result as not found.
Below is the implementation –
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.