AviralGarg.com

Sorting

1. Introduction to Sorting Problem

Definition

Importance in Computer Science

Applications Relevant to SDE Interviews

Duplicate Detection

Merging Sorted Files

Order Statistics

User Interface Applications

Benefits of Studying Sorting Algorithms

Learning Design Strategies

Algorithm Analysis

Interview Preparation

Approach to Learning Sorting Algorithms

Historical Example

IBM Card Sorter Machine

Summary


2. Brute Force: Selection Sort

Brute Force Design Strategy

Selection Sort Algorithm

Example with Characters

Pseudocode for Selection Sort

def selection_sort(A):
    n = len(A)
    for i in range(n):
        min_index = i
        for j in range(i + 1, n):
            if A[j] < A[min_index]:
                min_index = j
        # Swap the found minimum element with the first element
        A[i], A[min_index] = A[min_index], A[i]
    return A

Correctness Argument

Performance Analysis

Time Complexity

Space Complexity

Importance in Interviews

General Notes on Algorithm Analysis

Summary


3.Basics of Asymptotic Analysis

Quantifying Algorithm Running Time

Defining Running Time as a Function of Input Size

Basic Operations

Analyzing Selection Sort

Algorithm Overview

Counting Operations

Total Running Time

Asymptotic Analysis

Dominant and Lower-Order Terms

Simplifying Running Time

Implications of $ \Theta(n^2) $

Importance in Algorithm Analysis

Practical Application in Interviews


4. Brute Force: Bubble Sort

Introduction

Bubble Sort Algorithm

Algorithm Steps

  1. For each index $i$ from $0$ to $n - 1$:
    • Initialize a j pointer starting from index $n - 1$ and moving down to $i + 1$.
  2. For each position of the j pointer:
    • Compare $A[\text{j} - 1]$ and $A[\text{j}]$.
    • Swap if $A[\text{j} - 1] > A[\text{j}]$.
  3. Result:
    • After each pass, the next smallest element is placed at index $i$.
    • The sorted region grows from the left.

Pseudocode

def bubble_sort(A):
    n = len(A)
    for i in range(n):
        for j in range(n - 1, i, -1):
            if A[j - 1] > A[j]:
                # Swap adjacent elements
                A[j - 1], A[j] = A[j], A[j - 1]
    return A

Correctness Argument

Time Complexity Analysis

Comparison with Selection Sort

Practical Considerations

Notable Remarks

Summary


5. Decrease and Conquer Strategy and Insertion Sort

Algorithm Design Strategies

# Design Strategy Description Pros/Cons Examples
1. Brute Force - Directly based on the problem statement. ✅ Straightforward
❌ May vary between individuals due to subjectivity.
Selection Sort, Bubble Sort
2. Decrease and Conquer - Decrease the problem size ($n$) to size $n-1$. ✅: Systematic approach to problem-solving.
✅: Facilitates recursive thinking.
2.1 TOP-DOWN - Do upfront work to nibble away one (or few) element(s): $n \rightarrow n-1$

MAIN IDEA:
1. I pretend to be a Lazy Manager.
2. I nibble away at the problem to make it smaller.
3. I do upfront work of adding the nibble to the solution.
4. I hand over my solution and the remaining problem to my subordinate. (They do the same.)

CODING TIP: Write code from the perspective of an arbitrary lazy manager to sort the array:
..BEFORE: The managers above me would have ensured that all previous elements are sorted. (because they are lazy managers too!)
..MY JOB: I just extend the sorted region by one more element.
..AFTER: I hand over the remaining work to my subordinate. (They do the same.)
Selection Sort, Bubble Sort
2.2 BOTTOM-UP - Work at the end to extend the $(n-1)^{st}$ solution: $n-1 \rightarrow n$

MAIN IDEA:
1. I pretend to be a Lazy Manager.
2. I nibble away at the problem to make it smaller. I keep the nibble for work to be done later.
3. I hand over the remaining problem to my subordinate. (They do the same.)
4. The subordinate returns their solution to me.
5. I do the (later) work of adding my nibble to the solution.
6. I hand over my solution to my manager.
Insertion Sort

DESIGN STRATEGY #1: Brute Force

DESIGN STRATEGY #2: Decrease and Conquer

Decrease and Conquer Applied to Sorting

Top-Down Approach (Forward Decrease)

Bottom-Up Approach (Backward Decrease)

Insertion Sort Algorithm

Concept

Efficient Implementation

Example

Time Complexity Analysis

Key Points for Interviews

Summary