How to be Better at Algorithms? | Examples for Beginners


This article dives deep into the world of algorithms. It explains what algorithms are, how they work, and how they are used in various fields. It also provides examples of algorithms used in everyday life and discusses the efficiency of algorithms and the concept of Big O (n). The article further breaks down how algorithms are used to sort instruction lists and find the best possible solution or the optimal solution to a specific problem.

What is a Computer Algorithm?

An algorithm is a list of instructions that a computer can use to solve a certain problem. Algorithms are used in all fields of computing, and they are designed to solve problems in an efficient way.

The design of an algorithm depends on the complexity of the problem it needs to solve. For simple problems, brute force might be viable. However, for more complex issues, more complicated algorithms are needed.

Computer Algorithms are Everywhere

Algorithms are the backbone of all of our digital lives. They help us make decisions faster and more efficiently.

Examples of algorithms that are used in everyday life, such as Google search engine, Amazon’s recommendation system, and Netflix’s movie recommendation system, so algorithms are everywhere!

Algorithm Efficiency and the Big O(n):

The optimization process is also dependent on the type and complexity of the algorithm. In some cases, it can take days or weeks to optimize an algorithm while in others it can take only hours or minutes.

The time and space variables are the most important when it comes to Big O(n). Algorithms are an important part of software engineering and computing resources. One of the components that are crucial for their performance is the big O(n) notation. The letter O(n) in this phrase qualifies how many operations are being performed by a particular algorithm, where a lower number means faster execution times. Also, depending on how many (n)s we talking about. Big O focuses on the worst-case scenario.

The prominence of an operation is its efficiency in finding desired results. The size of an operation is its dependence on space to retrieve desired outcomes.

The more input on your operation the slower it will become. There are a few different common types of “Big O” notation which represent how the input affects the efficiency of an operation. These include O(n), O(1) which is the most efficient algorithm, and O(log n) and more…

How Do Algorithms Work?

Algorithms are used to sort lists of instructions and find the best possible solution or optimal solution for a given problem. The sorting algorithms are the most common algorithms used in data processing. They help organize data points by keeping it sorted so that it can be easily accessed.

The list of different methods for sorting algorithms is not complete without mentioning the linear sort, which is not really an algorithm but a technique that can be used with any of the other sorting methods.

Can be divided into simple and complex:

  • Simple sorting algorithms are called “bubble sort” and “insertion sort.”
  • Complex sorting algorithms include “quick sort,” “merge sort,” and “heap sort.”

The Complexity of an Algorithm

The algorithm can be measured in two ways:

Time Complexity for an Algorithms

Time complexity is the upper bound of the time taken by an algorithm to run. It is expressed in terms of a function, asymptotic notation, and a constant value. This may be written in the form of an equation, graph, or table to show the function’s dependence on the independent variable. A time complexity of O(n) means that the algorithm will complete a series of logical steps correlated to the n number.

Space Complexity for an Algorithms

Space complexity is a measure of how much space is required to execute an algorithm. Space Complexity for algorithms can be measured in a number of ways, including the number of iterations, the number of bits processed by each operation, or the number of words processed for each iteration. The O(n) algorithm is linear in the input size. This means that it takes time correlated to the size of the input, up to some maximum amount of work.

Multiple Techniques to Approach a Problem?

There are a variety of approaches to solving a problem, which can be classified using different criteria. Please note that this is just my opinion to classify the type of problem I’m trying to solve.

The most common types are:

  1. Divide and Conquer: are a clever way to tackle a problem. First, subdivide the problem into smaller subproblems, solve those smaller problems, and combine all the solutions into the problem’s final solution.
  2. Brute force: involve trying all possible solutions until a satisfactory solution is found.
  3. Randomized: use a random number during the computation to find a solution to the problem.
  4. Greedy: looking for an efficient solution on the smaller part, then expanding this optimum solution to the rest of the parts
  5. Recursive: are used to tackle larger and more difficult versions of a problem by solving smaller, simpler variations.
  6. Backtracking: is a computer programming technique that divides a problem into sub-problems, then takes each to an attempted solution. If the desired solution is not reached it will work backwards by finding a path that moves it forward in the problem.
  7. Dynamic Programming: focuses on solving complex problems in a series of simpler problems that are solved only one time rather than re-computing for each single problem. After at least computing it for once then you need to store it somewhere to recall it when necessary
  8. Pointer Traversal or Pathfinding: when searching a graph or network, it’s important to use a proven search algorithm. A pointer traversal is a type of search algorithm that can be used to find the shortest route from one node of the graph to another. For instance, if we want to find the shortest route from Node 1 to Node 2 using pointer traversal then we would start by looking for the nearest neighbor of Node 1 and then, if there is none, looking for the nearest neighbor of the node’s parent
  9. Hash Table: Hash table algorithms are used for a variety of purposes, such as collision detection and pathfinding. Collision detection is when you want to ensure that two objects don’t intersect with each other, while pathfinding is the process of calculating the shortest distance between two or more points.

We Got a Problem

How to Solve it in Plain English?

The first thing to do is read the problem and make sure that you understand what the instructions are asking you to do.

Next, identify possible values for all of the variables given in the problem, and try to come up with a logical solution for each variable.

Finally, try to write out an algorithm for, start with words not code, write what every programmer knows it’s called “Pseudocode“

What is a Pseudo Code?

When we think about programming, pseudo-code is often seen. It is a descriptive word with your language used by programmers to design an algorithm or another system’s functions.

This programming language can use the structural conventions of normal languages, yet it is meant for human reading and not machine reading.

Algorithm Example

Here is an example of how we think when we face a problem and how we solve it.

Of course, this is just my opinion, but I hope you were able to take something from it. I’ll get this problem from leetcode

This problem about Anagrams, An anagram is a word created by rearranging the letters of another word. The new word will always have all the original letters from the original word, but not in the same sequence.

Example 1

Input: strs = [“eat”,”tea”,”tan”,”ate”,”nat”,”bat”]

Output: [[“bat”],[“nat”,”tan”],[“ate”,”eat”,”tea”]]

You’ve read the description, seen the example, and done your research on Google. All of this should prepare you in advance for what is requested and what to include in your code lines.

Sure thing you’re still trying to figure out how to put this on code, but now that you have a sense for the solution, you can work on it.

My thoughts step-by-step procedure would be as follow:

1. I could get one instance of each word from the input data.

2. This one instance should make it the default one, and it should be sorted.

3. Again, this default instance would be the key value that collects other instances with.

4. This collection is an array that has these values coming from the sorted key value.

5. If I couldn’t find multiple instances then I will just return whatever instance I got.

Now let’s break it down even further by imagining that we write some lines of code. But this code is going to be a pseudo code.

So we need a kind of table that can store a key-value pair. The key would be the default sorted instance of a word and the values would be an array of these multiple instance values within the same characters of the same word. If we couldn’t find any other instances of the same word, then we put it in an array alone.


loop through the input array

sort each string to make it the default key that we talked about above

Is the key exists in the hashTable then put the unsorted version into its values

If not then create an array with the current value in the loop

Source Code in Javascript:

function groupAnagrams(strs) {

//creating the Hash Table

const hashTable = {}

  for (let i = 0; i < strs.length; i++){ //Loop through the input list
  // Creating the default key by sorting out the value from each string
  // To use the Sort method in JS, you need to use it on an array.
  // By using the Split() method, you create an array from this current value that you standing on.
  // By using the join() method, you recreate the string from the group of characters you created with the split() method above.
    const defKey = strs[i].split('').sort().join('')
    if (hashTable[defKey]){ 

  //Checking if the hash table has this value if yes, push the current value which is in that case going to be  different instance from the sorted default instance.
    } else {
    // if not just initialize it with an array
    hashTable[defKey] = [strs[i]]

//Another great method in JS to help you flat all arrays created in the values of hash Table into one array

return Object.values(hashTable)


Source Code in Python:

def groupAnagrams(strs):
  hashTable = {}
  for s in strs:
    defKey = ''.join(sorted(s))
    if defKey in hashTable:
      hashTable[defKey] = [s]

  return hashTable.values()


As we conclude, it’s clear that algorithms are not just a topic of interest for computer scientists and software engineers, but they are also a fundamental part of our everyday lives. They help us make decisions, solve problems, and even entertain us. Understanding algorithms, their efficiency, and their complexity is a crucial skill in the modern world.

This article has provided a comprehensive introduction to algorithms, from their basic definition to their application in various fields. It has also delved into the intricacies of algorithm efficiency, the concept of Big O notation, and how algorithms are used to sort instruction lists and find optimal solutions.

Reach out if you want to join me and write articles with the nerds 🙂

© 2024 · Nerd Level Tech


Social Media

Stay connected on social media