Sample Notes: Algorithms
A2 Level Computer Science – Detailed Notes
Topic 19.1: Algorithms (Computational Thinking and Problem-Solving)
Linear and Binary Searching Methods
Linear Search
- Definition: A sequential search algorithm where each element is checked one by one until the desired value is found or the end is reached.
- Pseudocode Example:
FOR i ← 0 TO length - 1 IF array[i] = target THEN RETURN i END FOR RETURN -1
- Best Case: First item is the target → O(1)
- Worst Case: Last item or not present → O(n)
- Use Case: Useful for unsorted data.
Binary Search
- Definition: Efficient search algorithm that divides a sorted array in half recursively to locate the target.
- Conditions: Data must be sorted.
- Pseudocode Example:
low ← 0 high ← length - 1 WHILE low ≤ high DO mid ← (low + high) DIV 2 IF array[mid] = target THEN RETURN mid ELSE IF array[mid] < target THEN low ← mid + 1 ELSE high ← mid - 1 END WHILE RETURN -1
- Best Case: Middle element is the target → O(1)
- Worst Case: Not found after log₂(n) comparisons → O(log n)
Insertion Sort and Bubble Sort
Insertion Sort
- How it Works: Builds the final sorted list one item at a time by comparing and shifting elements.
- Pseudocode:
FOR i ← 1 TO length - 1 key ← array[i] j ← i - 1 WHILE j ≥ 0 AND array[j] > key DO array[j+1] ← array[j] j ← j - 1 array[j+1] ← key
- Best Case (already sorted): O(n)
- Worst Case (reverse order): O(n²)
Bubble Sort
- How it Works: Repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
- Pseudocode:
FOR i ← 0 TO length - 1 FOR j ← 0 TO length - i - 2 IF array[j] > array[j+1] THEN SWAP array[j], array[j+1]
- Best Case (already sorted): O(n)
- Worst Case: O(n²)
Abstract Data Types (ADTs)
Overview
- Definition: A model for data structures that specifies type of data and operations but not implementation.
- Common ADTs:
- Stack
- Queue
- Linked List
- Binary Tree
- Dictionary
- Graph
Searching and Insertion Algorithms for ADTs
Linked List
- Find Item:
- Traverse from head node till item is found.
- Insert Item:
- Adjust next pointers to include new node at desired location.
- Delete Item:
- Bypass node to delete by changing pointer of previous node.
Binary Tree
- Find Item:
- Use recursive or iterative comparison from root.
- Insert Item:
- Follow left/right rules until empty space found.
- Delete Item:
- Remove node and restructure tree (3 cases: leaf, one child, two children).
Stack (LIFO)
- Insert (Push):
- Add item at top.
- Delete (Pop):
- Remove item from top.
Queue (FIFO)
- Insert (Enqueue):
- Add item at rear.
- Delete (Dequeue):
- Remove item from front.
Graph as an ADT
- Key Features:
- Consists of nodes (vertices) and edges
- Can be directed/undirected, weighted/unweighted
- Use Case: Representing networks, social graphs, road maps
- Implementation Not Required: Writing code for graph is not assessed.
Implementing ADTs from Built-in Types
- Stack: Use array/list with push and pop methods.
- Queue: Use circular array or list with enqueue/dequeue.
- Linked List: List of nodes with pointer to next.
- Dictionary: Associative array or hash table.
- Binary Tree: Node class with left and right child.
Comparing Algorithms
Performance Criteria:
- Time Complexity: How long algorithm takes (Big O notation)
- Space Complexity: How much memory is used
Big O Notation Summary:
Algorithm | Best Case | Average Case | Worst Case |
---|---|---|---|
Linear Search | O(1) | O(n) | O(n) |
Binary Search | O(1) | O(log n) | O(log n) |
Insertion Sort | O(n) | O(n²) | O(n²) |
Bubble Sort | O(n) | O(n²) | O(n²) |
- Usefulness of Big O: Enables evaluation of algorithm scalability.
Key Concepts Summary
- Linear search: Sequential search in unsorted data
- Binary search: Efficient, sorted data only
- Insertion & Bubble sort: Simple but inefficient for large data
- ADTs: Essential for structured data manipulation
- Graphs: Non-linear data structure with wide real-world use
- Big O: Formal notation to express algorithm efficiency