Build a simple algorithm

Build a simple algorithm

What is an algorithm?

Some people like to call it a recipe and I agree with them as long as you copy something, but if it’s a masterpiece from your thoughts then it’s called art!

An algorithm is something distinct from the programming language. You can write an algorithm in any programming language. You usually need to write algorithms in order to achieve some performance boost or solve some complex problems. The most common example or algorithm, in my opinion, is the sorting one.

How do you build an algorithm?

Taking the last example of sorting, we can make a simple example. Let’s say you have this list: var list = [5, 3, 7, 1, 8, 2] and you need to have this list ordered ascending.

To do this you create an algorithm

var list = [5, 3, 7, 1, 8, 2];
function sort(list) {
	// content
}
sort(list); // [1, 2, 3, 5, 7, 8]

An algorithm is a sequence of commands in a certain order to solve a problem.

Types of algorithms

  • Simple recursive algorithms
  • Backtracking algorithms
  • Divide and conquer algorithms
  • Dynamic programming algorithms
  • Greedy algorithms
  • Branch and bound algorithms
  • Brute force algorithms
  • Randomized algorithms

Solve your first algorithm

When you face a problem that you feel it might need an algorithm, the first thing you have to do is to try the Brute force option.

Let’s take again the last example of sorting and build a brute force version.

var list = [5, 3, 7, 1, 8, 2];

function sort(list) {
	for ( i = 0; i < list.length; i++ ) {
		for ( var j = 0; j < list.length; j++ ) {
			if (list[i] > list[j]) {
				swap(list[i], list[j]);
			}
		}	
	}
}

function swap(a, b) {
	var aux = a;
	b = a;
	a = aux;
}

sort(list); // [1, 2, 3, 5, 7, 8]

To test the performance of an algorithm, you need to think about millions of elements inside the list. With the current number of elements, this version will work just fine. The problem comes when you try to scale your program.

Saying that the list tends to grow to millions of elements and we need to start thinking about solutions.

The currently available solutions for sorting are: - Merge Sort - Insertion Sort - Bubble Sort - Quicksort - Heapsort

In this example, we’ll try to cover Bubble Sort. Coming soon, we’ll cover all of them with examples and demos.

for ( i = list.length; i <= 0 ; i-- ) {
    for ( j = 1; j <= i-1; j++ ) {
		if (list[j] > list[j+1]) {
			swap(list[j], list[j+1]);
		}
	}
}

Even though you still see 2 loops, one inside the other, the complexity of this algorithm is O(n). That means the list will be traversed only once ( n = the number of elements in the list ).

In most cases, you don’t need to write algorithms as programming languages these days have most of what you need. Including sort methods that are already optimized.

Why do I need to know algorithms?

Even though you might not use them, is good to know about them and have an idea of how to build one when needed. It is highly appreciated during the interviews and it increases your level of understanding.

Share on

See Also
Send us a few words.
What would you like to see different on this blog?
👌
👎