So, what are algorithms?

Algorithms are simply ways to describe how something is done. In the mathematical domain, this translates to theoretical mathematical functions and in programming, this translates to functions (most often) run by a computer. There are certainly other ways that algorithms could be explained but, in this course, we will focus on the ones used by programmers.

In this setting algorithms try to process data in some way, it may be to calculate a value from another, it may be to order/sort data in a certain way or it may be to find a specific piece of data from a large set of data. There are also situations when data should be translated, or a function mapped to a set of data, but we will start with functions to sort data.

There are many ways to sort a list of data, one naive solution to sort a list of numbers from lowest to highest would be to simply look at each pair of numbers and if one is higher the other switch their place and then continue doing that for the whole list, then start over again until the list is completely sorted. This algorithm is called the bubble sort and is one of the first algorithms that you usually learn when starting out. Other than this there are for example insertion sort, selection sort, merge sort and quick sort.

Just as with the data structures, depending on which sorting algorithm you use it will have consequences either in how much memory that is required or in how long it will take to operate. If we have N elements this would take N*N operations before the whole list was sorted in the worst case. In a merge sort, the list is restructured in recursively smaller parts, in this case by dividing each part of the list in half until there are only single elements. These elements are put together In the right order which allows it to be done in less time. If N elements are to be sorted using merge sort it would take N*log(N) operations at worst, which is a considerable increase compared to N*N. But as always there’s a drawback, the merge sort uses more resources in the form of allocated data than a bubble sort.

In the next article about algorithms you’ll learn how to design and partially how to implement the different algorithms mentioned in this article.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s