What is an algorithm?
Your thoughts?
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);
"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
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.
What is an algorithm?
What makes an algorithm good or bad?