Time Complexity of Linear and Binary Searches- Guessing Number Game for 2 Dimensions

·

2 min read

Following on in my usual make things better mindset. I have extended yesterday's simple idea and created code to work on two-dimension co-ordinates.

From the code, you will see there isn't a lot of changes.

The first change was doing it 'right' as in bringing the values from the functions back into the main module rather than printing the values out in the function. Yes, I was a bit lazy on that yesterday. This ensures, as it should, that the additional modules only perform one task of the number crunching and not the presentation.

There was a little tweak in the linear module to separate out the guess and the count variable. Even although they are the same in the linear check, it is neater not to just assume this.

linear.png

There were no changes to the halver module other than I changed the naming to the more correct binary. I thought halver gave a better description to someone that didn't understand what the binary methods were actually doing in halving the number of possible values with each iteration of the guess.

binary.png

All of the work in main.py wasn't a lot. Unpacking the tuples returned by the functions, changing the count variables to integers to allow calculations, and finally print out the results to compare the difference between linear and binary when it comes to time complexity.

main1.png

main2.png

Here is the folder in my Python Repo if you want the code. GitHub Repo