This is the beginning of my new series about algorithms.
Firstly let’s talk about Algorithms in general.
According to Wikipedia Algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation. Algorithms are always unambiguous and are used as specifications for performing calculations, data processing, automated reasoning, and other tasks.
Imagine that you are looking for someone with a surname starting with K on the phone book (old-school – I know). Surly you can start from the beginning of the book and looking for “K”, but probably you won’t do it like that. I’m sure you will open the book somewhere in the middle because you know that “K” is somewhere in the middle of the alphabet.
Now imagine that you are logging in to Twitter or any other social platform. When you are doing it you have to put your nickname and password. Twitter must check if you have an account and look into the database for your nickname. Let’s say that your nick is whoAmI. The platform can start looking from “A” but it will make more sense if it will start somewhere in the middle.
That’s the problem of binary searching. With the examples which I provide, we should use the very same algorithm which is binary searching.
Let’s look at how to write this algorithm in Python.
In the two last lines, I’m tasting my algorithm and printing to the console the output. As you can see in line 19 the output is 2. Remember that we are counting from 0. “None” in Python means that there is nothing there.
I hope that the explanation and examples which I put here are clear for you. Leave a comment if you have any questions or visit the Contact section on this page
Comments
Post a Comment