Introduction to algorithms
Video: Introduction to algorithmsThere 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.
- Where to go from here
Viewers: in countries Watching now:
Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.
- Writing source code
- Understanding compiled and interpreted languages
- Requesting input
- Working with numbers, characters, strings, and operators
- Writing conditional code
- Making the code modular
- Writing loops
- Finding patterns in strings
- Working with arrays and collections
- Adopting a programming style
- Reading and writing to various locations
- Managing memory usage
- Learning about other languages
Introduction to algorithms
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.
Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .
Here are the FAQs that matched your search "" :
- Q: Using TextEdit with Mac OS 10.9 Mavericks?
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.