- Algorithms,
or more specifically computer algorithms.
What are they?
And why are they of interest to you?
And why would they be useful to you,
in a life as a programmer?
Let's talk about that in today's video.
This video is brought to you
by the Learn Programming Academies, Java Masterclass,
with 77 hours of video training 320,000 students,
and 10s of thousands of glowing reviews.
This truly is the best way to learn Java.
Grab a massive discount and instant access by visiting
lpa.dev/YouTubeJava or by clicking
on the link in the description of this video.
(upbeat electronic music)
Welcome back, my name is Tim Buchalka
with another programming tip of the day.
So let's talk about algorithms, slash computer algorithms,
and why like would be of interest to you as a programmer.
So 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.
So 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 involve, 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 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,
and you can grab that algorithm, put that into your programme,
and you can then focus more on the programmes functionality
than having to solve specific issues
relating to, you know, a specific thing
that you're trying to do in the programme.
So if you can take a range of algorithms
and use those in your programmes,
it's going to save you a lot of time
'cause you're building on someone else's work,
that has been tested and proven
and adding that to your programmes.
Alright, so 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, you guess another number,
and I say no, and so on and so forth.
And 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.
So chances are you may hit it earlier than that,
but potentially, that's the worst case scenario
is it could take you the maximum range,
which is 100 in this case to guess.
Alright, So that's pretty efficient,
I think we generally agree with that.
So let's look at the binary search algorithm,
what it does to make this process a lot faster.
So what it does, 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.
Alright, so the computers 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 computers, guess of 50.
So at this point, the computers 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 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.
So you can see what's going on there.
In only two guesses, it's really starting
to hone in onto that number.
All right, so the next computers 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,
so 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.
Now, 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.
The, 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
99% of probability that the binary search
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.
So 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 programme
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 programme slow,
and you can see that that wouldn't be a good solution.
So 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 programmes to solve common problems.
And 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 programme,
without having to deal with common problems
that, you know, things like searching numbers,
for argument's sake, but the binary search
that is common to lots of programmes.
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.
Right, so hopefully, I've now sold you
on the benefits of algorithms.
So how do you learn algorithms?
Well take a course for example, one, 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 video,
and teaching 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;
and 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, agile 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 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 courses 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.
Alright, so next question you might have is
when should you start learning algorithms?
Is this something you should learn as a new programme?
Is it something you learned 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.
So 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.
And because you've got that basic Java expertise,
you'll understand algorithms a lot more.
Alright, 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.
Alrighty, so I hope that helped.
If you've got any questions, feel free
to leave a comment, and I'll get back to you.
If you're ready to look at the next tip,
click on up here and you can check that out.
If you're interested in coding specific programming videos,
click on the link in the bottom left hand corner,
consider subscribing by clicking on the link up here
and I'll see you soon!