Algorithm Analysis and Arrays
Algorithm Analysis: Time and Space Complexity, Big O Notation
**Time Complexity:** Time complexity measures the amount of time an algorithm takes to
run, based on the input size.
- Best Case (Ω): Minimum time taken.
- Average Case (Θ): Expected time.
- Worst Case (O): Maximum time taken.
Examples:
- Constant time: O(1)
- Linear time: O(n)
- Quadratic time: O(n^2)
- Logarithmic time: O(log n)
**Space Complexity:** Space complexity measures the amount of memory an algorithm
uses.
- Includes input space and auxiliary space (temporary variables, data structures, etc.).
Examples:
- If an algorithm uses a fixed number of variables: O(1)
- If it uses an array of size n: O(n)
**Big O Notation:** Describes the upper bound of time/space complexity as input size
grows.
- Used to classify algorithms by worst-case performance.
Arrays: Definition and Operations
**Definition:** Arrays are collections of elements stored in contiguous memory. Accessed
via indices.
**Operations on Arrays:**
- Insertion: Adding an element at a specific index. Time complexity: O(n) (worst case).
- Deletion: Removing an element at a specific index. Time complexity: O(n) (worst case).
- Traversal: Accessing each element. Time complexity: O(n).
**Multidimensional Arrays:** Arrays with more than one dimension (e.g., 2D matrix).
- Declaration: int arr[3][4];
- Access: arr[i][j] to access element at row i, column j.
**Applications of Arrays:**
- Linear data storage.
- Matrix operations.
- Sorting and searching.
- Basis for stacks, queues, heaps.
- Image representation.
Summary
Understanding algorithm analysis helps evaluate efficiency.
Arrays are essential for storing and manipulating data efficiently.