Definition of an Algorithm
An algorithm is a step-by-step, well-defined procedure for solving a problem
or performing a task. Algorithms are used in various fields like computer
science, mathematics, and data processing to achieve specific outcomes
efficiently.
Characteristics of a Good Algorithm
Finiteness: Must terminate after a finite number of steps.
Definiteness: Each step must be clear and unambiguous.
Input: Can accept zero or more inputs.
Output: Must produce at least one output.
Effectiveness: Each step should be basic enough to be carried out manually
or by a machine.
Types of Algorithms
Brute Force: Simple and straightforward, trying all possible solutions.
Example: Linear search.
Divide and Conquer: Breaks the problem into smaller sub-problems, solves
them independently, and combines the results.
Example: Merge Sort, Quick Sort.
Greedy Algorithm: Makes the best local choice at each step, aiming for a
global optimum.
Example: Dijkstra’s Algorithm.
Dynamic Programming: Solves sub-problems and stores the results to avoid
redundant calculations.
Example: Fibonacci sequence, Knapsack Problem.
Backtracking: Tries all possibilities and backtracks upon failure to find the
correct solution.
Example: N-Queens problem, Sudoku Solver.
Recursive Algorithm: Solves problems by calling itself with modified inputs.
Example: Factorial, Tower of Hanoi.
Common Algorithmic Problems
Sorting Algorithms:
Bubble Sort: Compares adjacent elements and swaps if necessary.
Merge Sort: Uses divide and conquer to sort.
Quick Sort: Partitions the array into subarrays and sorts recursively.
Searching Algorithms:
Linear Search: Sequentially checks each element.
Binary Search: Divides the sorted list and searches in halves.
Graph Algorithms:
Depth-First Search (DFS): Explores as deep as possible along a branch before
backtracking.
Breadth-First Search (BFS): Explores all neighbors at the current depth before
moving deeper.
Time Complexity
Time complexity refers to how the execution time of an algorithm increases
with input size.
Big O Notation: Describes the upper limit of an algorithm’s runtime.
O(1): Constant time.
O(log n): Logarithmic time.
O(n): Linear time.
O(n log n): Log-linear time.
O(n²): Quadratic time.
Space Complexity
Space complexity refers to the amount of memory an algorithm needs
relative to the input size.
Examples
Factorial Algorithm (Recursive)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
Binary Search Algorithm
def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Applications of Algorithms
Search Engines: Use search algorithms to retrieve relevant data.
Data Compression: Algorithms like Huffman coding reduce data size.
Machine Learning: Uses algorithms to build predictive models.
Cryptography: Ensures secure data communication using encryption
algorithms.