Friday, July 4, 2025

🚀 Day 5: Merge Sort & Quick Sort in Java 🎯 Goals for Today


  • Understand the Divide and Conquer strategy

  • Learn and implement:

    • Merge Sort

    • Quick Sort

  • Analyze Time & Space Complexities

  • Solve intermediate-level sorting problems


📘 1. Merge Sort

A stable, divide-and-conquer sorting algorithm.
Time Complexity: O(n log n) in all cases
Space Complexity: O(n) (extra array)

✅ Logic:

  • Divide the array into two halves

  • Sort each half recursively

  • Merge the two sorted halves

🔹 Java Implementation


public class MergeSort { public static void mergeSort(int[] arr, int left, int right) { if (left < right) { int mid = (left + right) / 2; // Sort the left and right halves mergeSort(arr, left, mid); mergeSort(arr, mid + 1, right); // Merge them merge(arr, left, mid, right); } } public static void merge(int[] arr, int left, int mid, int right) { int n1 = mid - left + 1; int n2 = right - mid; int[] L = new int[n1]; int[] R = new int[n2]; for (int i = 0; i < n1; i++) L[i] = arr[left + i]; for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j]; int i = 0, j = 0, k = left; while (i < n1 && j < n2) { if (L[i] <= R[j]) arr[k++] = L[i++]; else arr[k++] = R[j++]; } while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; } }

⚡ 2. Quick Sort

Quick and efficient, especially on average cases.
Time Complexity:

  • Best/Average: O(n log n)

  • Worst: O(n²) (when pivot is worst chosen)
    Space: O(log n) (due to recursive calls)

✅ Logic:

  • Choose a pivot

  • Partition the array: elements < pivot go left, > pivot go right

  • Recursively sort left and right

🔹 Java Implementation


public class QuickSort { public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; // Index of smaller element for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; // Swap int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // Swap pivot to the right place int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } }

📊 3. Merge Sort vs Quick Sort

FeatureMerge SortQuick Sort
Time (Best)O(n log n)O(n log n)
Time (Worst)O(n log n)O(n²)
SpaceO(n)O(log n)
Stable✅ Yes❌ No
In-place❌ No✅ Yes
Use CaseLinked listsArrays

🧠 4. Practice Problems

Try these problems:

  1. Merge Sorted Array

  2. Sort an Array

  3. 🔁 Kth Largest Element in Array – Use Quick Select

  4. 🧠 Count Inversions in Array – Use Merge Sort logic


📚 Homework for Day 5

  • Implement Merge Sort and Quick Sort

  • Solve 2 LeetCode problems involving sorting or partitioning

  • Try to write non-recursive quick sort (advanced)

  • Visualize Merge vs Quick: Try https://visualgo.net/en/sorting


🔜 Coming Up on Day 6:

  • Recursion Mastery

  • Backtracking intro (Subset, Permutations)

  • Understanding call stack and dry runs

No comments:

Post a Comment

How we can get higher marks in semester exam

 Here we talk about how to get higher marks in exams or test paper. Now we have to remember that the test and exams are follow the pattern b...