What is an algorithm?

Your thoughts?

|

What is an algorithm?

  • An algorithm follows a procedure to get a desired output (think recipe)
  • An algorithm is necessary because it's A LOT more efficient than not following a procedure. To continue the recipe concept, imagine trying to make a new dish simply by seeing the end result and experimenting from scratch versus following a set of rules. The latter is going to be a lot faster even though you will eventually get there by experimenting.
  • Algorithms are also necessary because computers have finite resources. Computers only have so much memory and processing power, so developing algorithms to efficiently use these resources is a MUST.
  • An algorithm follows a finite number of steps. Algorithms are never functions that run indefinitely like an infinite while loop or something like that.
  • Algorithms are independent of code or programming languages. It's not like you write an algorithm that only works with Java but not another programming language. A programming language is how you implement an algorithm. Remember that an algorithm is just a procedure or set of instructions at the end of the day.

What makes an algorithm good or bad?

  • Complexity is measured by space and time
  • Time obviously relates to how long the algorithm takes to solve the problem
  • Space refers to how much memory it takes to run the algorithm or the size of the inputs
  • Algorithms are measured based on best and worst case scenarios. Upper and lower bounds are used to defined to analyze how well an algorithm works.
|

great understanding what IS an algo

|

Here is an example of a basic algorithm:

function findMax(arr) {
  if (arr.length === 0) {
    return "Array is empty";
  }


  let max = arr[0]; // Assume the first element is the maximum


  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i]; // Update max if a larger element is found
    }
  }


  return max; // Return the maximum number in the array
}


// Example usage:
const numbers = [3, 7, 2, 9, 1, 5];
const maxNumber = findMax(numbers);
console.log("The maximum number is:", maxNumber);


|

awesome yeah this is a good start. also important to remember that it IS NOT language specific. You can implement this same algo in different languages.

It IS NOT a JavaScript algorithm :).

|

"At it's core, an algorithm is a set of step-by-step instructions designed to solve a specific problem or perform a task." - ChatGPT

|

lol accurate and true...let the algo explain the algo :)

|

Also remember too that an algorithm is a way of thinking or approaching a problem. Algorithmic thinking involves breaking down a problem into manageable steps that perform consistently.

As humans, we solve things intuitively without even thinking about it. For example, if you are given a list of numbers and asked to big the biggest number, you quickly look at the list and find it.

But what if you can't see? Writing instructions for a computer to find the biggest number is a lot different than just innately "knowing" what to do.

This is where algorithmic thinking comes in. It's explicitly defining steps to solve a problem like finding the biggest number.

|

important to remember that there are all sorts of different algorithms like sorting algorithms, searching algorithms, back tracking algorithms, divide and conquer, greedy, etc.