Learn and Play with Algorithm

in programming •  6 years ago 

An algorithm is a set of instructions. The definition is really that simple. An algorithm can be as easy as giving instructions like this:

Go down the street.
Take the first right.
Find the second house on the left.
Knock on the door.
Deliver the package.

But while the definition of algorithm is simple, the actual meaning and how it affects our lives can be quite complex.

An Example of an Algorithm
A common example of an algorithm that we use in our daily lives is a recipe. This set of instructions gives us all of the ingredients we will need and directions on what to do with those ingredients. Sounds easy, right?

But what if you don't know where the measuring cup is kept? You'd need an algorithm to find it. You may even need an algorithm on how to use a measuring cup.

So while an algorithm is a set of instructions, it also needs to take into account who or what is going to interpret those instructions. For example, If you give directions to a friend detailing how to get from your house to the nearest grocery store, your friend will only know how to get to that store if they know where your house is located. They're not capable (yet) of finding that particular grocery store from say, another friend's house.

This is how an algorithm can be both simple and complex. And when we talk in terms of computer algorithms, understanding what a computer is capable of doing is a fundamental part of formulating algorithms.

How Sorting Algorithms Evolved
One of the earliest algorithms created was the bubble sort routine. Bubble sort is a method for sorting numbers, letters or words by looping through a data set, comparing each set of values side-by-side, and swapping them when needed.

This loop is repeated until the algorithm can move through the entire list without needing to swap anything, which means the values are sorted correctly. This type of algorithm is often referred to as a recursive algorithm because it loops on itself over and over until it completes the task.

That type of algorithm might look as simple as:

Go to the first value.
Check that value against the next value and swap positions if needed.
Go to the next value and repeat the comparison.
If we are at the end of the list, go back to the top if any value was swapped during the loop.

But bubble sort didn't turn out to be the most efficient way of sorting values. As time went on and computers became more capable of doing complex tasks quickly, new sorting algorithms popped up.

One such algorithm scans through the first list and creates a second list of sorted values. This method only makes a single pass through the original list, and with each value, it will loop through the second list until it finds the correct place to put the value. Usually, it is more efficient than using the bubble sort method.

This is where algorithms can get really crazy. Or really interesting, depending on how you look at it.

While the bubble sort method is considered one of the most inefficient methods of sorting values in many ways, if the original list is presorted properly, bubble sort can be one of the most efficient. That's because, in that instance, the bubble sort algorithm will go through the list a single time and determine it is correctly sorted.

Unfortunately, we don't always know if our list is presorted, so we have to choose an algorithm that is going to be the most efficient to use on average across a large number of lists.

What We Learn From the Bubble Sort Algorithm
Algorithms evolve over time as we find more efficient ways to do things and/or computers become more capable of carrying out complex tasks.
Algorithms are sometimes chosen because they are "more" efficient "most" of the time.
Just because an algorithm is more efficient most of the time doesn't mean its always the best in each individual case.
Facebook Algorithms and More in Everyday Life
Algorithms are at work helping humans every day. When you search the web, an algorithm is at work trying to find the best search results. Ask your smartphone for directions, and an algorithm decides the best route for you to take. And when you browse Facebook, an algorithm decides which of our friend's Facebook posts are most important to us. (Let's hope our friends don't find out which one Facebook thinks we like the most!)

But thinking algorithmically can help us far beyond our computer lives. It can even help us build a better sandwich.

Let's say I start with two slices of bread, spreading mustard on one slice and mayonnaise on another slice. I put a slice of cheese on the bread with the mayonnaise, some ham on top of that, some lettuce, two slices of tomato and then cap it with that slice with the mustard on it. Good sandwich, right?

Definitely if I eat it right away. But if I leave it on the table for a while, that top slice of bread might get soggy from soaking up some of that tomato. It's a problem I didn't quite anticipate, and I might make sandwiches for years before noticing, but once I do, I can start thinking of ways to change my algorithm in order to build a better sandwich.

For example, I could get rid of the tomato. But I don't want to lose that tomato taste. So instead, I can put the tomato on the sandwich after the bread and the lettuce. This allows the lettuce to form a protective barrier between the tomato and the bread.

This is how an algorithm evolves. And an algorithm doesn't have to be run by a computer to be an algorithm. An algorithm is a process, and processes are all around us.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://www.lifewire.com/what-is-an-algorithm-4153813