Introduction to DAA
What is an Algorithm?
An algorithm is a finite, well-defined sequence of steps that solves a specific problem or performs a computation. The word comes from the name of the 9th-century Persian mathematician Muhammad ibn Musa al-Khwarizmi, whose work on arithmetic was translated into Latin as "Algoritmi".
Think of an algorithm as a recipe — it takes inputs (ingredients), follows a precise set of instructions, and produces an output (the dish). Every program you write is built on algorithms.
What is DAA?
Design and Analysis of Algorithms (DAA) is the study of:
- Design — how to construct efficient algorithms to solve problems.
- Analysis — how to measure the efficiency of algorithms in terms of time and space.
DAA is a core subject in computer science because the same problem can be solved by many algorithms — but some are dramatically faster or use far less memory than others. For example, sorting 1 million numbers takes 1 second with Merge Sort but over 11 minutes with Bubble Sort.
Characteristics of a Good Algorithm
A well-designed algorithm must satisfy all of the following properties:
| Property | Description | Example |
|---|---|---|
| Input | Zero or more well-defined inputs | An array of numbers to sort |
| Output | At least one output for every valid input | The sorted array |
| Finiteness | Must terminate after a finite number of steps | Cannot loop forever |
| Definiteness | Every step must be clear and unambiguous | "Compare arr[i] and arr[j]" — not "compare some elements" |
| Effectiveness | Every instruction must be feasible and executable | No "divide by zero" or impossible operations |
| Correctness | Produces the correct output for all valid inputs | Sorted array is actually sorted |
| Efficiency | Uses minimal time and memory resources | O(n log n) instead of O(n²) |
Types of Algorithms
| Type | Description | Examples |
|---|---|---|
| Searching | Find an element in a data structure | Linear Search, Binary Search |
| Sorting | Arrange elements in order | Bubble Sort, Merge Sort, Quick Sort |
| Graph | Traverse or find paths in graphs | BFS, DFS, Dijkstra, Kruskal |
| Divide and Conquer | Split problem, solve parts, combine | Merge Sort, Binary Search |
| Dynamic Programming | Store solutions to overlapping subproblems | Knapsack, LCS, Floyd-Warshall |
| Greedy | Make locally optimal choices | Dijkstra, Huffman Coding |
| Backtracking | Try all possibilities, undo bad choices | N-Queens, Sudoku |
| Randomized | Use randomness for efficiency | Randomized Quick Sort, Monte Carlo |
Algorithm vs Program
| Algorithm | Program |
|---|---|
| Language-independent (pseudocode or flowchart) | Written in a specific programming language |
| Design phase — what to do | Implementation phase — how to do it |
| Must be finite | Can run indefinitely (e.g., OS, web server) |
| Analyzed for correctness and efficiency | Tested for correctness and performance |
| Hardware independent | Depends on hardware and OS |
Writing an Algorithm — Pseudocode
Pseudocode is an informal, language-independent way to describe an algorithm. It uses plain English mixed with programming constructs. There is no strict syntax — the goal is clarity.
/*
* PSEUDOCODE: FindMax
* ─────────────────────────────────────────
* Input: Array A of n integers
* Output: Maximum element in A
*
* Algorithm FindMax(A, n):
* 1. max ↠A[0]
* 2. FOR i ↠1 TO n-1 DO
* 3. IF A[i] > max THEN
* 4. max ↠A[i]
* 5. END IF
* 6. END FOR
* 7. RETURN max
* ─────────────────────────────────────────
*/
public class FindMax {
public static int findMax(int[] arr) {
int max = arr[0]; // Step 1
for (int i = 1; i < arr.length; i++) { // Step 2
if (arr[i] > max) { // Step 3
max = arr[i]; // Step 4
}
}
return max; // Step 7
}
public static void main(String[] args) {
int[] arr = {3, 7, 1, 9, 4, 6, 2};
System.out.println("Array: {3, 7, 1, 9, 4, 6, 2}");
System.out.println("Max = " + findMax(arr)); // Max = 9
}
}
/*
* Trace for arr = {3, 7, 1, 9, 4}:
* i=0: max = 3 (initial)
* i=1: arr[1]=7 > 3 → max = 7
* i=2: arr[2]=1 < 7 → max = 7
* i=3: arr[3]=9 > 7 → max = 9
* i=4: arr[4]=4 < 9 → max = 9
* Return 9 ✓
*
* Analysis:
* - Time Complexity: O(n) — visits every element once
* - Space Complexity: O(1) — only one extra variable (max)
*/
Why Study DAA?
- Performance matters: A slow algorithm on a billion records can take hours; a fast one takes seconds.
- Cost reduction: Efficient algorithms reduce server costs and improve user experience.
- Career: DAA is the foundation of competitive programming, system design, and technical interviews at top companies.
- Better decisions: Understanding complexity helps you choose the right data structure and algorithm for the job.
- Scalability: An O(n²) algorithm that works fine for 1,000 records will fail for 1,000,000.
Algorithm Complexity — Quick Overview
We measure algorithm efficiency using Big-O notation, which describes how the running time grows as input size n increases:
| Notation | Name | n=10 | n=100 | n=1000 |
|---|---|---|---|---|
| O(1) | Constant | 1 | 1 | 1 |
| O(log n) | Logarithmic | 3 | 7 | 10 |
| O(n) | Linear | 10 | 100 | 1,000 |
| O(n log n) | Linearithmic | 33 | 664 | 9,966 |
| O(n²) | Quadratic | 100 | 10,000 | 1,000,000 |
| O(2â¿) | Exponential | 1,024 | 10³Ⱐ| 10³â°Â¹ |