Algorithms - What are they and Why should I care about them?

Algorithms - What are they and Why should I care about them?

Let's start with the definition

In mathematics and computer science, an algorithm is a finite sequence of rigorous instructions, typically used to solve a class of specific problems or to perform a computation.

If you are someone like me.Then that definition made little to no sense, on the first read. So here is an easier definition.

A set of instructions.

Yep! that's it.

If you have ever followed a recipe from a cookbook, then that's an algorithm. You read the instructions given in the cookbook (input), then you carry out those instructions (Do Something) and you get your dish (Output).

The same are computer algorithms, you input something the computer runs a set of instructions on them and gives you the output.

Why Should i care about what an algorithm does?

7-Things-to-do-if-stock-markets-crash_1200x499.png On the 6th of May 2010, 9% of the entire United States market just disappeared in 5min (Also known as the Flash crash of 2:45), and nobody to this day can even agree on what happened. This was done by algorithms but what happened nobody knows.
Why does nobody know the reason you ask?. The same reason why you don't know how google gives the best search results, the same reason why you don't know how the flight fares are so drastically different when booking the same flight from two different regions.
A bunch of algorithms running somewhere deciding what to give you, based on the input they have collected. So, it becomes very important for us to understand them.

Lets write an algorithm

Let's start with a simple algorithm. This algorithm tells us whether an applicant has exceeded the age limit for a job position or not.

Before writing the actual algorithm, let's write something known as Pseudocode.

Pseudocode is a plain language description of the steps in an algorithm.

PseudoCode
Age = (Input from the User);
if( Age < 50 )
then
print("You can apply for this position" )
else
print("Sorry you cannot apply for this positon" )
end

That was pretty easy to understand right.

Now let's write it in a programming language.

//JAVA
int a = in.nextInt();
if(a < 50){
System.out.println("You can apply for this position" );
} else {
System.out.println("Sorry you cannot apply for this positon"  );
}

Use cases of algorithms

5188.webp Algorithms are used everywhere. When I say everywhere I actually mean everywhere. Social media, Search Engines, Flights, Cars, Elevators, attends systems, online booking systems, Traffic lights, etc. On a personal level, I love the algorithm which is used to solve Sudoku.
Wait you want to know why I love the algorithm used for solving Sudoku. Well when I was in school, we use to get newspapers every day and we had to read them, to tell you the truth reading newspaper can be boring, but what made it fun was the Sudoku section.
But the problem was that it took a long long long long long time to solve a Sudoku puzzle.
So, how can you become the coolest kid in the class, by solving the Sudoku the fastest, this is how I did it.

public class SudokuSolver {
    public static void main(String[] args) {
        int[][] board = new int[][]{
                {3, 0, 6, 5, 0, 8, 4, 0, 0},
                {5, 2, 0, 0, 0, 0, 0, 0, 0},
                {0, 8, 7, 0, 0, 0, 0, 3, 1},
                {0, 0, 3, 0, 1, 0, 0, 8, 0},
                {9, 0, 0, 8, 6, 3, 0, 0, 5},
                {0, 5, 0, 0, 9, 0, 6, 0, 0},
                {1, 3, 0, 0, 0, 0, 2, 5, 0},
                {0, 0, 0, 0, 0, 0, 0, 7, 4},
                {0, 0, 5, 2, 0, 6, 3, 0, 0}
        };
        if (solve(board)) {
            display(board);
        } else {
            System.out.println("Cannot solve");
        }
    }
    static boolean solve(int[][] board) {
        int n = board.length;
        int row = -1;
        int col = -1;

        boolean emptyLeft = true;

        // this is how we are replacing the r,c from arguments
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 0) {
                    row = i;
                    col = j;
                    emptyLeft = false;
                    break;
                }
            }
            // if you found some empty element in row, then break
            if (emptyLeft == false) {
                break;
            }
        }

        if (emptyLeft == true) {
            return true;
            // soduko is solved
        }

        // backtrack
        for (int number = 1; number <= 9; number++) {
            if (isSafe(board, row, col, number)) {
                board[row][col] = number;
                if (solve(board)) {
                    // found the answer
                    return true;
                } else {
                    // backtrack
                    board[row][col] = 0;
                }
            }
        }
        return false;
    }

    private static void display(int[][] board) {
        for(int[] row : board) {
            for(int num : row) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
    static boolean isSafe(int[][] board, int row, int col, int num) {
        // check the row
        for (int i = 0; i < board.length; i++) {
            // check if the number is in the row
            if (board[row][i] == num) {
                return false;
            }
        }

        // check the col
        for (int[] nums : board) {
            // check if the number is in the col
            if (nums[col] == num) {
                return false;
            }
        }

        int sqrt = (int)(Math.sqrt(board.length));
        int rowStart = row - row % sqrt;
        int colStart = col - col % sqrt;

        for (int r = rowStart; r < rowStart + sqrt; r++) {
            for (int c = colStart; c < colStart + sqrt; c++) {
                if (board[r][c] == num) {
                    return false;
                }
            }
        }
        return true;
    }
}

This must have not made much sense to you, if you don't know about Backtracking and Recursion, but that's not the point. I just wanted to show you that an algorithm is nothing but a step of instructions.

That's it. Hope you had a fun read.

Resources used:-
What's an algorithm? - David J. Malan
How algorithms shape our world - Kevin Slavin
Sudoku Solver