Posts

Showing posts with the label Java Programming

Insertion Sort — Theory & Code

Image
Insertion Sort is a simple, intuitive algorithm ideal for small or nearly sorted datasets. In this blog post, we’ll delve deep into the workings of Insertion Sort—covering the core concept, step-by-step visualization, Java implementation, and performance evaluation. 1. What is Insertion Sort? Insertion Sort builds a sorted array one element at a time. You pick the next element from the unsorted portion and insert it into its correct position within the sorted portion, shifting elements as needed. 2. Step-by-Step Illustration Unsorted array: [5] [2] [4] [6] [1] [3] Iteration 1: Sorted: [5] Take next: 2 → insert before 5 Array becomes: [2] [5] [4] [6] [1] [3] Iteration 2: Sorted: [2] [5] Next: 4 → shift 5 to right, insert 4 → [2] [4] [5] [6] [1] [3] Continue until sorted. Diagram (ASCII): Initial: 5 | 2 4 6 1 3 Step 1: 2 5 | 4 6 1 3 Step 2: 2 4 5 | 6 1 3 Step 3: 2 4 5 6 | 1 3 ... Final: 1 2 3 4 5 6 3. Java Implementat...

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 ...

Cycle Sort in Java — An In-Depth Guide

Image
When preparing for interviews at top companies like Amazon, Google, and Microsoft , you will often encounter problems that require efficient in-place sorting . One such elegant yet lesser-known algorithm is Cycle Sort . Unlike traditional sorting algorithms, Cycle Sort minimizes the number of writes, making it highly efficient when write operations are costly. 🔹 What is Cycle Sort? Cycle Sort is an in-place, comparison-based sorting algorithm. It works by placing each element directly in its correct position using the concept of cycles . Once an element is placed in the right spot, we continue the cycle until the entire array is sorted. Key Features: Minimizes the number of write operations (best choice when memory writes are expensive). Performs sorting in-place with constant space complexity (O(1)). Has a time complexity of O(n²), which makes it less practical for large datasets. 🔹 How Cycle Sort Works Let’s break the algorithm into simple steps: Sta...

ArrayList vs LinkedList in Java

Image
Java’s Collections Framework provides two commonly used List implementations: ArrayList and LinkedList . While both implement the List interface , they differ in their underlying data structures, performance, and use cases. Let’s break down their differences with examples and diagrams. 1. Underlying Structure ArrayList → Backed by a dynamic array. Elements are stored in contiguous memory . LinkedList → Implemented as a doubly-linked list. Each node stores data + pointers to the previous and next node. Diagram: ArrayList (Dynamic Array) [ 0 ] → [ 1 ] → [ 2 ] → [ 3 ] → [ 4 ] (index based access) LinkedList (Doubly Linked Nodes) NULL ← [0 | *] ↔ [1 | *] ↔ [2 | *] ↔ [3 | *] → NULL 2. Code Example ArrayList Example: import java.util.*; public class ArrayListExample { public static void main(String[] args) { List list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("C+...

Java Streams Terminal Operations Explained with Examples

Image
In our previous post, we explored intermediate operations in Java Streams. Now, let’s move to terminal operations . These are the final operations that produce a result (like a value, collection, or side-effect) and terminate the stream pipeline. 1. forEach() The forEach() method is used to perform an action for each element of the stream. List<String> words = Arrays.asList("apple", "banana", "cherry"); words.forEach(word -> System.out.println("item is " + word)); 2. collect() The collect() method gathers the elements of a stream into a collection (like a List, Set, or Map). List<Integer> nums = Arrays.asList(1,1,2,2,2,3,4,5,5,5,6); Set<Integer> res = nums.stream().collect(Collectors.toSet()); System.out.println(res); 3. reduce() The reduce() method performs a reduction on the elements of the stream using an accumulation function. List<Integer> nums = Arrays.asList(1,2,3,4,5); Optional<Integer...