How to Write Algorithms for Beginners

Devising new algorithms can seem intimidating to new programmers, but it's a skill that can be practiced like any other. Start by looking for a book of algorithm problems for beginners or by taking an online or offline computer science class. Practice mastering the basics of algorithm design, including estimating complexity and runtime, checking for edge cases that could cause trouble for a computer algorithm, and breaking problems into smaller parts.

Image Credit: vgajic/E+/GettyImages

What a Computer Algorithm Is

An algorithm is a procedure that a computer or a human follows to solve a problem. Long division is a sample algorithm that many people learn to do in school. The Euclidean algorithm, used to find the greatest common divisor of two numbers, is another common example.

A computer algorithm is ultimately written in a programming language that the computer can understand, but when the algorithm is being developed, programmers and computer scientists often write it first informally as prose and then more formally in a generic format called pseudocode.

Pseudocode looks like a programming language, but because it's designed to be read by humans rather than computers, it doesn't have rigorous syntactic rules.

Simple Algorithm Examples for Beginners

Famous examples of algorithms are often taught to beginning computer scientists and programmers. Some examples are Dijkstra's Algorithm, which is used in graph theory to find the shortest path between two points; Merge Sort, which is used to sort lists of data; and the RSA Algorithm used to encrypt data. Many of these are available online in free textbooks, videos and course material.

The online learning site Khan Academy has many examples of algorithms that beginners can experiment with. Major universities like Harvard, Stanford and the Massachusetts Institute of Technology make curricular materials and course videos with common algorithms available online for introductory computer science classes.

There are also sites with programming contest problems and explanations of how they're solved, which can help people interested in developing their skills.

Algorithm Considerations

When you're coming up with a new algorithm, you want to make sure it works in all cases where you think it should and try to understand how efficient it is. Typically, programmers divide the algorithm into discrete parts so they can think about how each part works and how long it takes. This is called modular design.

It's a good idea to test an algorithm yourself with pen and paper on some simple cases before beginning to write code. When you think about efficiency, think about the average case, common situations your algorithm is likely to encounter and the worst-case runtime. The worst-case runtime is often represented with what's called Big-O Notation.

references