Algorithms, or more specifically computer algorithms: what are they and why are they of interest to you? Why would they be useful to you in your life as a programmer? Let's talk about that today.
Let's talk about algorithms/computer algorithms, and why they could be of interest to you as a programmer.
Starting out with a definition, Wikipedia states that an algorithm is an unambiguous specification of how to solve a class of problems. It goes on to say that the algorithm can perform things like calculations, processing, automated reasoning, and a host of other things. Now, I won't go into the history of algorithms other than just to say a few quick things.
Firstly, the word algorithm has its origins dating way back to the ninth century, and also the modern concept of the algorithm really started taking shape from the late 1920s. So algorithms have been around for a while.
Talking about computers specifically, what is a computer algorithm? Well, think of it as a formula for solving a specific problem. That's really what it comes down to, and yes, it is really that simple. However, implementing a computer algorithm can be complex and that involves or might involve many steps or a sequence of steps in order to get that to work.
All right, so we know what an algorithm is and what computer algorithm is. Why are computer algorithms useful to you as a programmer? Well again, they're designed to solve a specific problem so rather than you having to reinvent the wheel, you can actually use an algorithm that someone else, perhaps someone smart - usually someone smart - has already figured out the most optimal and efficient way to implement or solve a particular problem. You can grab that algorithm, put that into your program, and you can then focus more on the programs functionality than having to solve specific issues relating to a specific thing that you're trying to do in the program. So if you can take a range of algorithms and use those in your programs, it's going to save you a lot of time because you're building on someone else's work, that has been tested and proven and adding that to your programs.
Let's look at an example now of an algorithm, a computer algorithm, just to get our head around what, or how this can be practical. So let's look at the binary search algorithm. Now the purpose of a binary search is to search for a range of numbers, and basically in the most optimal and time efficient way. So you really wanna get to the number that you're searching for as quickly as possible.
Now let's start looking at linear search, which is the slower way to search a range of numbers. So to do that, let's just say that I've asked you to guess a number that I'm thinking of in the range of one to 100. So we're gonna go through a linear search first to figure that out. So you might, if you're following along, you might guess number five is the first number. I would say "No", you guess another number. I say, "No" again and you guess another number, and I say "No", and so on and so forth. Depending on how many guesses you take and how lucky you are, it could take you up to the maximum range to guess the number. In other words, if the range in this case was between one and 100, it may well take you 100 guesses to guess the number that I was thinking, unless you were lucky to hit it earlier. Chances are, you may hit it earlier than that but potentially, the worst case scenario is it could take you the maximum range, which is 100 in this case to guess. So that's pretty efficient, I think we generally agree with that.
Let's look at the binary search algorithm and what it does to make this process a lot faster. So what it does, is it takes the approach of choosing the number in the middle of the range and repeatedly asking if the number that it's guessed is higher or lower. Now, that's higher or lower than the number that I'm thinking of. So as you go through the same example again, so let's just assume that the same rate, the number number ranges between one to 100, and the number that I'm thinking is say 65. So the computer's first guess taking it halfway, it's going to get 50, the number 50. That's obviously halfway between the range one to 100. Now I say "higher" to the computer because 65, my number is higher than the computer's guess of 50. So at this point, the computer's second guess is honing in on that number, because it's now gonna use a range of 50 to 100 instead of one to 100, because it knows we've already told it that the number is higher than 50. So therefore, the computer's second guess is gonna be between that new range, and the new range again is 50 to 100. So the computer's second guess is 75 this time. Now obviously 75 is higher than my number, so I say "lower", 65 is lower than 75. So the computer now knows that the number is between the ranges of 50 to 75. You can see what's going on there. In only two guesses, it's really starting to hone in onto that number. So the next computer's guess is right about halfway between those two numbers, and you're not always going to get an exact match so it might be slightly lower, depending on the number of ranges, it might be not evenly in the halfway mark, if you know what I'm saying. So 63 will be the next guess, now I say "higher", So the next guess, because obviously 63 is the halfway range between 50 and 75, and 65 is higher than 63. So at this point, now the computer is getting really close. So the fourth guess, the computer is gonna choose again between that range, that new range of 63 to 75, halfway between that and 69. And basically it's really honing in onto the number there, so I say "lower". So at this point now the computer is really getting to the stage where it pick the number because it's got a range now of 63 to 69. So basically the computer is gonna pick the number now, in the next turn or perhaps one turn after. So let's see a five or six guesses, usually seven guesses. It will get pretty close to solving the problem for most ranges of numbers within reason, obviously, but that's really how binary search algorithm is actually working.
You can see that that's significantly more efficient, in general, than the linear search that we used before where we're just using random numbers, while going through each number in that sequence using a linear search is gonna be more efficient. Now, that's not always the case, though. Basically what it comes down to, the binary search is usually more efficient, and I say potentially, well, usually, but it does depend on what it's searching for. Certainly, for a large range of numbers is going to be more efficient, or it is more efficient, and it's really only not going to be the case if you just randomly, you're lucky enough using a linear search to guess the number that's being searched for.
The other thing to keep in mind though with a binary search is the number sequence that you're going through has to be sorted so the numbers have to be sorted in order for the binary search to work and basically, you can see that this is pretty efficient though. So if you had a need in your computer program to actually search through a large range of numbers, you can see that a binary search would be extremely useful to you and what you could do is you could just grab that algorithm, and literally place that into your project, and it's problem solved.
If you had a problem you needed to solve, to search that range of numbers in the most efficient way, you can just use an algorithm rather than you having to think about it, "Oh heck, how am I going to do this?" Or maybe you didn't know how to do that, and you'd do a search a linear search which would end up being pretty slow, that makes your whole computer program slow and you can see that that wouldn't be a good solution.
The point here is that the algorithm, in this case, the binary search algorithm, is great at solving a specific problem and what you can do is start learning other algorithms, and eventually, you've got a toolkit of algorithms that you can start using, and applying into your programs to solve common problems. There's tonnes of algorithms out there to solve a huge range of problems so once you start using them, you can really start reducing the amount of time it takes to create projects because you're focusing on the functionality for your specific computer program without having to deal with common problems, things like searching numbers, for argument's sake, but the binary search that is common to lots of programs.
Now in terms of computer languages, algorithms can be coded in most computer languages, and some computer languages are better at implementing algorithms than others. But in general, most of our modern programming languages are great and will work fine with most algorithms. Hopefully, I've now sold you on the benefits of algorithms.
So how do you learn algorithms? Well, take a course for example. One method is to take a course like my Data Structures and Algorithms course. It goes through a range of algorithms and data structures (which I'll talk about in another post) and teaches you how to use them and what they are, how they work, and how to implement them. That's one approach. Another approach is just to grab a book. There's computer books on algorithms, there's probably hundreds (I haven't counted them).
Another great way though, if you don't wanna do that is just to search YouTube or Google in general and as a hint there, add your language, when you're searching for the code for that particular problem. So for example, if you knew that you wanted to use a binary search, just do a search for "binary search Java" or 'binary search Python", if one of those two languages was the language that you're looking for and almost certainly you'll a find code out there where someone has taken the algorithm and they've actually implemented in your chosen language. Now, obviously, you'd need to know that you're searching for. You need to know that, in this particular case, it was a binary search you're looking for, but you'll learn that by going through your typical programming data structures and algorithms course. It will teach you what each one is and you can just sort of take it from there or just really by doing some general researching for algorithms, you'll find lots and lots of information about algorithms.
All right, so next question you might have is when should you start learning algorithms? Is this something you should learn as a new program? Is it something you learn later on down the track? Well, in general, you want to have an understanding of programming first. So you would want to go through a typical programming course first or learn the basics of programming before you learn algorithms and that's because the algorithms are actually written in the target language (usually that you're learning and using) so you need to have a good understanding of that programming language in order to be able to understand that algorithm, and to be able to implement it.
In other words, learn the basics of the programming language that you're looking at, or working with first, before getting into algorithms next. So I would generally suggest you go through something like for example, my Java Masterclass (if you're learning Java), you would take that class first, then after that, you swing over to a course like the Data Structures and Algorithms course, which will then take your understanding of Java, and you can then start applying it to algorithms. Because you've got that basic Java expertise, you'll understand algorithms a lot more.
All right, so that was it, algorithms. I hope you got a lot out of it, and start implementing algorithms is my firm advice moving forward for you, to get the most out of your career options, and good luck with it.
So I hope that helped. If you've got any questions, feel free to leave a comment, and I'll get back to you.