# Algorithm Examples

Algorithms provide computers with a series of instructions that transform data into usable knowledge. Every algorithm is essentially trying to make a decision, often as part of a series of decisions, to ensure a computational input is processed and transmitted as usable data based on the task it's trying to accomplish.

### Example 1 - Standard Addition Algorithm

• Line up the numbers vertically along matching place values.
• Add numbers along the shared place value columns.
• Write the sum of each place value below each place value column.
• If the place value column sums over nine, carry the tens digit over to the next column to the left.
• Once all place value columns have been added, the addition is complete and the algorithm terminates.

Here is an example showing this algorithm:

Notice that the numbers are aligned vertically along place values. Then, each place value column is added, and the result is written below the horizontal line.

### Example 2 - Standard Subtraction Algorithm

• Line up the numbers vertically along matching place values.
• Subtract the numbers along the shared place value columns.
• Write the difference of each place value below each place value column.
• If the number at the top of a column is smaller than the number below it, regroup before subtracting.
• Once all place value columns have been subtracted, the subtraction is complete and the algorithm terminates.

Here is an example showing this algorithm:

### Example 3: Find the largest number among three numbers

• Start
• Declare variables a,b and c.
• Read variables a,b and c.
• If a > b
• If a > c
• Display a is the largest number.
• Else
• Display c is the largest number.
• Else
• If b > c
• Display b is the largest number.
• Else
• Display c is the greatest number.
• Stop

### Example 4: Find Roots of a Quadratic Equation ax2 + bx + c = 0

• Start
• Declare variables a, b, c, D, x1, x2, rp and ip;
• Calculate discriminant
• D ← b2-4ac
• If D ≥ 0
• r1 ← (-b+√D)/2a
• r2 ← (-b-√D)/2a
• Display r1 and r2 as roots.
• Else
• Calculate real part and imaginary part
• rp ← -b/2a
• ip ← √(-D)/2a
• Display rp+j(ip) and rp-j(ip) as roots
• Stop

### Example 5 - The Sieve of Eratosthenes

This algorithm was derived by Eratosthenes to find all the prime numbers in a table of numbers. This algorithm involves finding all the numbers greater than two and crossing out the ones that are divisible by two. Repeat this process for non-crossed out numbers greater than three and onto infinity until every non-prime number is crossed out.

Let's see at an example of the Sieve of Eratosthenes algorithm:

1. Create a boolean array prime[0..n] and initialize all entries as true.
prime[i] will be true if i is a prime number, and false otherwise.
2. Set the first two elements (prime and prime) as false since 0 and 1 are not prime.
4. While p * p <= n:
If prime[p] is true (indicating p is a prime number), proceed; otherwise, move to the next number.
5. Mark all multiples of p as composite (not prime):
Starting from p * p, incrementing by p, mark prime[i] as false.
6. Move to the next unmarked number greater than p.
7. Repeat s 4-6 until there are no more unmarked numbers remaining.
8. The remaining unmarked numbers in the prime array are the prime numbers up to n.

## This is the summary of the algorithm:

• It starts with the assumption that all numbers from 2 to n are prime.
• It iterates over each prime number (p), marking all its multiples as composite.
• After the algorithm is completed, the unmarked numbers are the prime numbers.

[2, 3, 5, 7, 11, 13]

In this table, the only test numbers used are from two to fifteen; however, most of these are trivial since the sieve is finished after three. Moreover, it is helpful to remember that when testing if numbers are divisible by n, only look at at numbers strictly greater than n; otherwise, the sieve will show certain numbers are composite when they are actually prime.

## Consider the below algorithms:

1. Binary Search: An algorithm used to efficiently search for a specific element in a sorted list. It repeatedly divides the search space in half by comparing the target value with the middle element until the target value is found or the search space is exhausted.
2. Bubble Sort: A simple sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order. It continues this process until the entire list is sorted.
3. Dijkstra's Algorithm: A graph traversal algorithm used to find the shortest path between two nodes in a weighted graph. It starts at a selected source node and iteratively explores the neighboring nodes, updating the minimum distance to reach each node until the destination node is reached.
4. QuickSort: A popular sorting algorithm that follows the divide-and-conquer approach. It selects a pivot element from the list and partitions the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. It then recursively sorts the sub-arrays.
5. Depth-First Search (DFS): A graph traversal algorithm that explores as far as possible along each branch before backtracking. It starts at a selected node and visits all the neighbors of that node before proceeding to its unvisited neighbors, recursively.
6. Breadth-First Search (BFS): A graph traversal algorithm that explores all the vertices of a graph in breadth-first order, i.e., it visits all the vertices at the same level before moving to the next level. It uses a queue to keep track of the vertices to be explored.
7. Merge Sort: A sorting algorithm that employs the divide-and-conquer strategy. It divides the unsorted list into sub-lists, each containing a single element, and then repeatedly merges the sub-lists to produce new sorted sub-lists until a single sorted list is obtained.
8. Knapsack Problem (Dynamic Programming): An algorithmic problem that involves selecting items from a set, each with a weight and value, to maximize the total value while keeping the total weight below a certain limit. Dynamic programming can be used to solve this problem efficiently.
9. Backtracking: This involves finding the most efficient solution by back tracking various paths.

These are some of the algorithm examples.

## Applications:

1. Sorting: Algorithms like Quicksort, Merge Sort, and Heap Sort are widely used for sorting data in various applications such as database management, data analysis, and information retrieval.
2. Searching: Algorithms like Binary Search and Hashing are used to efficiently search for elements in sorted arrays or data structures. Search algorithms are used in databases, file systems, and web search engines.
3. Graph Algorithms: Graph algorithms such as Breadth-First Search (BFS), Depth-First Search (DFS), Dijkstra's algorithm, and Prim's algorithm are fundamental in solving problems related to network routing, social network analysis, recommendation systems, and more.
4. Dynamic Programming: Dynamic programming algorithms, such as the Fibonacci sequence or the Knapsack problem, are used to solve optimization problems by breaking them down into smaller overlapping subproblems and building up solutions incrementally.
5. Computational Geometry: Algorithms in computational geometry, such as Convex Hull algorithms or Line Intersection algorithms, are used in computer graphics, computer-aided design (CAD), robotics, and geographic information systems (GIS).
6. Machine Learning: Various machine learning algorithms, such as Support Vector Machines (SVM), Random Forests, and Neural Networks, are used in a wide range of applications, including image and speech recognition, natural language processing, and recommendation systems.
7. Encryption and Cryptography: Algorithms like RSA, AES, and SHA are used for secure communication, data encryption, digital signatures, and protecting sensitive information.
8. Network Algorithms: Algorithms related to network flows, routing, and optimization play a crucial role in designing efficient network architectures, traffic routing, and load balancing in computer networks.
9. Image and Signal Processing: Algorithms such as Fast Fourier Transform (FFT), image compression algorithms (JPEG), and edge detection algorithms are used in image and signal processing applications, including medical imaging, video streaming, and audio processing.
10. Game Theory: Algorithms and techniques from game theory are used in various fields, including economics, decision-making, resource allocation, and artificial intelligence, to model and analyze strategic interactions between agents.

### Feedback   