Posts

Showing posts with the label Big O Notation

Selection Sort — Theory & Code

Image
Selection Sort is one of the simplest sorting algorithms and a popular choice for learning how sorting works “under the hood.” This post goes beyond the standard explanation—covering the algorithm’s logic, visual steps, Java code, and complete performance analysis. 1. What is Selection Sort? Selection Sort sorts an array by maintaining two regions: Sorted subarray (initially empty, grows from the left). Unsorted subarray (initially the whole array). In each iteration, it picks the minimum element from the unsorted portion and swaps it with the element at the current boundary, thus growing the sorted part by one.:contentReference[oaicite:4]{index=4} 2. Step-by-Step Illustration Unsorted: [64] [25] [12] [22] [11] Sorted: [] Iteration 1: Find min from unsorted → 11 Swap 11 with 64: Sorted: [11] Unsorted: [25] [12] [22] [64] Iteration 2: Find min → 12 Swap with 25: Sorted: [11][12] Unsorted: [25][22][64] Continue similarly until fully sorted. Diagrammatic ...

Time and Space Complexity

Image
Every programmer eventually comes across the terms time complexity and space complexity . These concepts are critical in writing efficient programs and are fundamental in coding interviews. In this post, we’ll dive deep into Big O , Big Ω , and Big Θ , understand how to analyze algorithms, and illustrate everything with diagrams and examples. 1. Why Do We Need Complexity Analysis? Imagine two algorithms solving the same problem. One takes 1 second for 1000 inputs, while the other takes 10 seconds . As input size grows, the difference becomes massive. Complexity analysis gives us a way to predict performance without running the program on all possible inputs. Diagram: Growth Rates Input size (n) → O(1): ■■■■■■■■■ (constant) O(log n): ■■■■■■■■■■■■■■ (slow growth) O(n): ■■■■■■■■■■■■■■■■■■■■■ (linear growth) O(n²): ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ (steep) O(2^n): skyrockets! 2. Big O, Big ...