In its most basic form, an algorithm is a set of detailed step-by-step instructions to complete a task. For example, an algorithm to make coffee in a french press would be:
- Pour water into the kettle, close the lid, and turn it on.
- Take the lid off the french press and pour in 17 grams of ground coffee.
- When the water in the kettle is boiling, pour 290 grams of hot water into the french press.
- Put the lid of the french press back on with the plunger up.
- Wait 4 minutes.
- Gently press the plunger down until it reaches the bottom.
- Pour coffee into a mug.
In computer science, common algorithms have names like "Quicksort" and "Bogosort". Algorithms are often grouped into different categories like search, sorting, and compression algorithms. Further, algorithms can be described by the approach it takes to complete a task, such as recursive, backtracking, divide and conquer, greedy, and brute force.
Algorithms are often paired with data structures, though they are fundamentally different. Data Structures are methods of storing data so that an algorithm can perform operations on it easily.
Some common examples of data structures are arrays, stacks, queues, linked lists, trees, graphs, hash tables, and heaps.
Algorithms are often judged and compared based on their efficiency and the resources they require. One of the most common ways to evaluate an algorithm is to look at its time complexity through a method called Big O notation.
Big O notation is a way to describe the speed or complexity of an algorithm, and shows the worst case number of operations for a given input size. It's important to understand the possible run time for different algorithms, especially when working with large or growing data sets. Big O notation makes it easier to choose the right algorithm for each task.
Sorting algorithms come in various flavors depending on your necessity. Some, very common and widely used are:
No sorting discussion is complete without mentioning Quick Sort.
The Merge Sort algorithm relies on splitting and sorting smaller arrays before merging them into one sorted array.
freeCodeCamp’s curriculum heavily emphasizes creating algorithms. This is because learning algorithms is a good way to practice programming skills. Interviewers most commonly test candidates on algorithms during developer job interviews.