Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There is a term you will come across in programming called an algorithm and when you hear it described, it sounds similar to a program itself. An algorithm is a series of steps to accomplish a task, but usually when we are talking about it in programming, we mean a small procedure, part of a program, rather than the whole program itself. Perhaps even just a function. Yes, it does something specific, but there is an understanding that there may be many different ways to accomplish the same task, many different algorithms.
So let me give you an example. Let's say we have a series of numbers we need to sort. It could be 5. It could be 5000. Now, there is a whole bunch of different ways we could do this, but here is one of them. I am going to compare two numbers at a time. So I will take the first two numbers and compare them. If the first one is bigger than the second, I am going to flip them. Then I will move up and I'll compare the next two. Again, if the first is bigger than the second, we will flip them. In this case, it's not. I will then move up again, compare the next two.
Is the first bigger than the second? Yes, it is. And taking them step-by-step means we can take the biggest number on the first pass and move it all the way up to the top. So when we get to the end of this first pass, we know that the highest number is at the end. However, they are still not all in order. We need to make another pass. So we go back to the start, compare those two numbers. If the first one is higher than the second, we will flip them. It's not, but then the second to it is, so we will flip those, and move up again, and we will flip those.
Now, this time I know I don't need to go all the way to the end, because I already bubbled up the highest number there. So we need to go to the last number, but one, we are already there. So I can just return to the start. Again, comparing the numbers, I don't need to flip those two. I do need to flip those two. And after multiple passes, I won't need to flip anymore. If I go through a password, I don't need to flip anything, I know they're all in order. This is what's referred to as a bubble sort. It's a simple sorting algorithm.
We make multiple passes and bubble the larger numbers up to the top of the list. And here's the pseudo code for this. It really isn't all that bad. You can write a bubble sort in just a few lines of code, and it wouldn't matter whether you fed five numbers into it or 5000 or 5 million. It's a simple straightforward algorithm, and unfortunately, it's almost always an absolutely terrible choice, because bubble sorts are so inefficient. If you have to work with large lists of data, they're one of the most inefficient ways of sorting that that you can imagine.
Well, the question might be, well, are there other ways of doing this? Well, absolutely. The question might b,e how long have you got? There are many sorts, bubble sorts, heap sorts, quick sorts, insertion sort, merge, selection, permutation sorts, and these are just some of the best-known and most famous sorting algorithms. There are many more than this. And one of the great things as you'll find that in most languages now, there are sorting functions already built into the language. When we are working with arrays or other collections, you can almost expect them to have a built-in sort functionality to them, that's already been written been written, that's already been configured and tested, but it's the idea here that's important.
The simplest algorithms to think of and write are not necessarily the most efficient. Having said that the most efficient algorithms might be really hard to write, tough to understand, tough to maintain, and this is one reason why computer programming has often been called an art rather than a science. Because if you give ten experienced programmers the same problem, you're likely to get 10 completely different solutions. So understand that there never is just one way to solve a problem. The first way that comes to mind may work, but there may be better ways and in programming it's often a tough choice deciding between ease of use, ease of development, speed, and elegance.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101483 Viewers
61 Video lessons · 88250 Viewers
71 Video lessons · 72117 Viewers
56 Video lessons · 103881 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.