What is an algorithm?

Author Portrait
Lefteris OikonomouMarch 18 2024

Certainly, you've heard the term "algorithm." The word "algorithm" is often associated with an abstract concept, as something complex or related to mathematics. However, in reality, it's much simpler.

What is an Algorithm

To define an algorithm, you need a starting and an ending state.

An algorithm is a series of commands that take you from a starting to an ending state.

The starting state is also called the input of the algorithm. The ending state is called the output of the algorithm.

Algorithm

Algorithm Example

Imagine you wake up in the morning and want to drink orange juice. Starting state (Input): You want to drink orange juice. Ending state (Output): You have a glass of orange juice in your hands.

Algorithm:

  1. Material preparation:

    1. Find two or more ripe oranges.

    2. You have a juicer or an electric orange juicer.

    3. Prepare a glass or container to collect the juice

  2. Preparing the oranges:

    1. Cut the oranges in half horizontally (from the stem to the base).

  3. Juicing:

    1. Take half of an orange and place it on the juicer.

    2. Apply pressure by pressing and turning the orange on the juicer to extract as much juice as possible.

    3. Repeat the process for the other half of the orange and for all the oranges you have.

  4. Juice collection:

    1. Pour the juice from the juicer container into your glass.

Thus, the algorithm is the process that leads you from the initial state (oranges) to the final state (orange juice).

Why We Need Algorithms

Algorithms are useful because they offer solutions to problems. Whether you want to make a dessert, tie your shoelaces, or sort numbers from smallest to largest, you need an algorithm.

Once someone creates an algorithm, they can share the solution with you. This way, not everyone has to solve the same problem.

This is particularly useful in computer science, where algorithms play the most crucial role. For example, computing systems use an algorithm that efficiently sorts numbers billions of times daily.

What is a Good Algorithm

For an algorithm to be good, it must do the following:

  • Correctly solve the problem.

    It must give us the correct result (output) for every possible input.

  • Be efficient.

    In programming, an algorithm's efficiency is critical. It’s measured in terms of time complexity (how the execution time changes as the input size changes) and space complexity (how much memory it uses).

  • Be scalable.

    Meaning it can work efficiently and quickly for both small and large inputs. For example, a sorting algorithm works quickly and correctly for 2 and 2.000.000 million numbers.

Algorithms You Use Daily

  • Dijkstra:

    This algorithm finds the shortest of all possible paths to go from point A to point B. It is used by the navigation systems you use every day.

  • Cryptography Algorithms:

    It is used to keep your data safe, whether we're talking about your conversations on social media or your e-banking.

  • PageRank:

    This is the algorithm Google uses to rank websites in your search results.

  • Recommendation Algorithms:

    Analyze your behavior and preferences to make suggestions. Netflix uses them to suggest series to you, Spotify to suggest songs, and e-shops to suggest related products.

  • Facial Recognition Algorithms:

    Allows you to unlock your mobile phone with your face (FaceID).

  • Social Media Algorithms:

    Used by social networking platforms to decide what you'll see on your homepage.

  • Weather Forecasting Algorithms:

    Analyze historical data and current weather conditions to find patterns and accurately predict the weather for the coming period.

algorithm

What is an Algorithm in Programming

In programming, an algorithm is a precise set of instructions designed to lead us from a starting to an ending state. The definition is the same as the one we discussed above. The only difference is that in programming, algorithms are executed by computers.

The algorithm can range from simple data calculations to complex problem-solving operations.

All programming algorithms must be written in Programming Languages. Algorithms must be expressed in a language that computers can interpret, such as Python, Java, or C++. Each language has its syntax and style, but the core logic of the algorithm remains constant across languages.

Algorithms are the building blocks of software. Whether a social media platform sorts posts by relevance or a navigation app calculates the quickest route, algorithms drive the functionality.

Algorithm Design Techniques

Name

Method

Example

Brute Force

Tries all possible solutions

Password Cracking

Recursion

Converts the problem to recursive relation

Calculating factorial

Hashing

Utilizes hash functions

Quick lookup

Divide and Conquer

Divides a problem into sub-problems

Sorting numbers

Greedy Algorithms

Makes the optimal choice at each step.

Finding Shortest path

Dynamic Programming

Saves repeating calculations

Fibonacci sequence

Randomness

Uses chance

Monte Carlo methods

  • Brute Force: The algorithm tries all possible solutions until it finds the right one. It's easy to think about and understand. The more possible solutions there are, the slower the algorithm becomes.

  • Recursion: The algorithm breaks the problem into smaller parts, which it solves based on a recursive relation.

  • Hashing: The algorithm transforms data into a value using a hash function. This transformation allows us to find and modify data quickly. It is often used in databases.

  • Divide and Conquer:

    This strategy divides a problem into sub-problems, solves each independently, and then combines their solutions. It’s a powerful method for tackling complex problems by simplifying them.

  • Greedy Algorithms:

    These algorithms make the most optimal choice at each step, aiming for a local optimum in hopes of finding a global optimum. While efficient, they're not universally applicable to all problems.

  • Dynamic Programming:

    Saves results of intermediate steps to avoid recalculating them, significantly speeding up the computation for problems with overlapping subproblems.

  • Randomness:

    Incorporates chance into the decision-making process, which can be particularly effective when a good-enough solution is acceptable or deterministic solutions are impractical.

Relevant Posts

Examples and Applications of Artifical Intelligence
Tech5 minutes read
Examples and Applications of Artifical Intelligence

Discover everything about Artificial Intelligence (AI) - its definition, applications. Learn how AI is transforming industries and what it means for the future.

Author Portrait
Lefteris OikonomouMarch 2 2024
Type of Developers | Focus Area, Skills, Salaries
Tech7 minutes read
Type of Developers | Focus Area, Skills, Salaries

Explore the different developer types. From Fronted to AI and security. Uncover their key skills, the challenges of landing the first job, and salary insights. Guide from an insider!

Author Portrait
Lefteris OikonomouFebruary 9 2024
How to get your First Web Development Job in 2024 - (From a Web Developer)
Tech12 minutes read
How to get your First Web Development Job in 2024 - (From a Web Developer)

Looking for a developer role? Discover the ultimate guide to landing your first developer job in 2024, packed with insider tips, strategies, and real-world advice.

Author Portrait
Lefteris OikonomouFebruary 5 2024