An algorithm is a procedure used to solve a problem or perform a certain calculation. They are a core component of any computer program built into operating systems to ensure that our devices obey the correct commands and deliver the correct results on demand. In the algorithmic approach , the most appropriate method is selected for the solution , and what needs to be done is revealed step by step , and the order of these steps should be considered . In fact, we encounter algorithms in all areas of our lives. To give an example from our daily life, when cooking, the steps in the recipe of that dish are actually an algorithm.
Types of algorithms:
1. Brute Force Algorithm
It is one of the simplest algorithms. Especially if we compare it to the recursive algorithm. As the name suggests, it tries all the ways to the end until it solves the problem and hopes to find the solution. Such algorithms can be used to find the best solution since all solutions have been tried. At the same time, it is a long solution as it has tried all the solutions.
For example; sequential search
2. Simple Recursive Algorithm
It solves the problem by itself. Until it reaches the solution, it calls itself with a smaller value each time and reaches the solution. That is, it breaks the problem into smaller pieces and makes them smaller problems. And it continues until the smallest state. When the problem is solved in the smallest case, the solution of the general problem is reached. It is used for cases that are of the same type and can be divided into parts. It is a complex algorithm because it is in a structure that constantly shrinks and repeats itself.
For example; Towers of Hanoi, Inorder/Postorder tree circulations
3. Backtracking Algorithm
The backtracking algorithm solves problems step by step, and if it solves any step incorrectly, it returns that step and tries another path for that step. Here, it is aimed to find the most correct way among many ways. There is no guarantee that the best path will be found in this algorithm. Because the algorithm does not try all the ways, it just goes back from the wrong step and tries another way. The result is reached after all the steps have been selected with the aim of being the best option.
For example; Queens problem, sudoku
4. Searching Algorithm: Searching algorithms are the ones that are used for searching elements or groups of elements from a particular data structure. They can be of different types based on their approach or the data structure in which the element should be found.
5. Sorting Algorithm: Sorting is arranging a group of data in a particular manner according to the requirement. The algorithms which help in performing this function are called sorting algorithms. Generally sorting algorithms are used to sort groups of data in an increasing or decreasing manner.
6. Hashing Algorithm: Hashing algorithms work similarly to the searching algorithm. But they contain an index with a key ID. In hashing, a key is assigned to specific data.
7. Divide & Conquer Algorithm
It is similar to the recursion algorithm. This algorithm first divides the problem in two and then in half again. Thus, until the problem is solved, it repeatedly divides it into smaller parts and tries to solve it when it becomes the smallest. For the general solution of the problem, all parts are combined again and a solution is obtained.
For example; quick sort, sort merge
8. Greedy Algorithm
This algorithm approaches the result piecemeal and chooses the results that are closest to the solution, that is, it can benefit immediately. It is aimed to choose the best one among the options leading to the solution and to find the best solution to the problem in general. While making a choice, the closest choice to the solution is made regardless of whether it is the best choice or not. Therefore, this choice may not always be the best solution.
For example; Huffman coding, Dijkstra algorithm
9. Dynamic Programming Algorithm
This algorithm remembers the results of past studies and uses them to find new results. The dynamic programming algorithm reduces the problem to simple sub-problems and solves them. It stores these answers for later use.
For example; Fibonacci sequence
10. Randomized Algorithm: In the randomized algorithm we use a random number so it gives immediate benefit. The random number helps in deciding the expected outcome.
Let's take an example of a computer algorithm. Let's write the algorithm that displays the average of the four numbers entered by the user:
A0 --> Start
A1 --> Counter=0 (The first number of Counter starts as 0.)
A2 --> Number=? : T=T+Number (Enter number. Add number to T and show T.)
A3 --> Counter=Counter+1 (Add 1 to Counter and show counter.)
A4 --> If Counter<4, go to A2. (If the counter is less than 4, go to Step 2.)
A5 --> O=T/4 (Divide T by 4 for the mean)
A6 --> Show O. (Show average.)
A7 --> Stop