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 ← b24ac
 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 rpj(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 noncrossed out numbers greater than three and onto infinity until every nonprime number is crossed out.
Let's see at an example of the Sieve of Eratosthenes algorithm:
 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.
 Set the first two elements (prime[0] and prime[1]) as false since 0 and 1 are not prime.
 Start with the first prime number, p = 2.
 While p * p <= n:
If prime[p] is true (indicating p is a prime number), proceed; otherwise, move to the next number.
 Mark all multiples of p as composite (not prime):
Starting from p * p, incrementing by p, mark prime[i] as false.
 Move to the next unmarked number greater than p.
 Repeat s 46 until there are no more unmarked numbers remaining.
 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:
 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.
 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.
 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.
 QuickSort: A popular sorting algorithm that follows the divideandconquer approach. It selects a pivot element from the list and partitions the other elements into two subarrays, according to whether they are less than or greater than the pivot. It then recursively sorts the subarrays.
 DepthFirst 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.
 BreadthFirst Search (BFS): A graph traversal algorithm that explores all the vertices of a graph in breadthfirst 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.
 Merge Sort: A sorting algorithm that employs the divideandconquer strategy. It divides the unsorted list into sublists, each containing a single element, and then repeatedly merges the sublists to produce new sorted sublists until a single sorted list is obtained.
 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.
 Backtracking: This involves finding the most efficient solution by back tracking various paths.
These are some of the algorithm examples.
Applications:
 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.
 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.
 Graph Algorithms: Graph algorithms such as BreadthFirst Search (BFS), DepthFirst 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.
 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.
 Computational Geometry: Algorithms in computational geometry, such as Convex Hull algorithms or Line Intersection algorithms, are used in computer graphics, computeraided design (CAD), robotics, and geographic information systems (GIS).
 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.
 Encryption and Cryptography: Algorithms like RSA, AES, and SHA are used for secure communication, data encryption, digital signatures, and protecting sensitive information.
 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.
 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.
 Game Theory: Algorithms and techniques from game theory are used in various fields, including economics, decisionmaking, resource allocation, and artificial intelligence, to model and analyze strategic interactions between agents.
