Episode 7 — DSA with JavaScript / 7.9 — Sorting Algorithms

7.9.b — Merge Sort, Quick Sort & Cyclic Sort

<< 7.9 Overview


Merge Sort

Divide array in half, sort each half, merge sorted halves.

Merge sort tree for [38, 27, 43, 3, 9, 82, 10]:

           [38, 27, 43, 3, 9, 82, 10]
          /                           \
    [38, 27, 43, 3]            [9, 82, 10]
    /             \             /         \
 [38, 27]     [43, 3]      [9, 82]     [10]
  /    \       /    \       /    \
[38]  [27]  [43]   [3]   [9]  [82]

Merge back up:
[27, 38]  [3, 43]  [9, 82]  [10]
   [3, 27, 38, 43]    [9, 10, 82]
      [3, 9, 10, 27, 38, 43, 82]

JavaScript

function mergeSort(arr) {
    if (arr.length <= 1) return arr;
    const mid = Math.floor(arr.length / 2);
    const left = mergeSort(arr.slice(0, mid));
    const right = mergeSort(arr.slice(mid));
    return merge(left, right);
}

function merge(a, b) {
    const result = [];
    let i = 0, j = 0;
    while (i < a.length && j < b.length) {
        if (a[i] <= b[j]) result.push(a[i++]);
        else result.push(b[j++]);
    }
    return result.concat(a.slice(i)).concat(b.slice(j));
}

C++

void merge(vector<int>& arr, int l, int m, int r) {
    vector<int> tmp(r - l + 1);
    int i = l, j = m + 1, k = 0;
    while (i <= m && j <= r) tmp[k++] = arr[i] <= arr[j] ? arr[i++] : arr[j++];
    while (i <= m) tmp[k++] = arr[i++];
    while (j <= r) tmp[k++] = arr[j++];
    for (int x = 0; x < k; x++) arr[l + x] = tmp[x];
}

void mergeSort(vector<int>& arr, int l, int r) {
    if (l >= r) return;
    int m = l + (r - l) / 2;
    mergeSort(arr, l, m);
    mergeSort(arr, m + 1, r);
    merge(arr, l, m, r);
}

Time: O(n log n) always | Space: O(n) | Stable: Yes


Quick Sort

Choose a pivot, partition into smaller/larger halves, recurse.

Quick sort trace for [3, 6, 8, 10, 1, 2, 1]:

Pivot = 1 (last element):
  [1] [3, 6, 8, 10, 2, 1] → partition around 1
  
After partition: [1, 1, 8, 10, 6, 2, 3]
  Left: [1], Pivot: 1, Right: [8, 10, 6, 2, 3]
  
Continue recursively...

JavaScript

function quickSort(arr, lo = 0, hi = arr.length - 1) {
    if (lo >= hi) return arr;
    const pivot = arr[hi];
    let idx = lo;
    for (let i = lo; i < hi; i++) {
        if (arr[i] < pivot) {
            [arr[i], arr[idx]] = [arr[idx], arr[i]];
            idx++;
        }
    }
    [arr[idx], arr[hi]] = [arr[hi], arr[idx]];
    quickSort(arr, lo, idx - 1);
    quickSort(arr, idx + 1, hi);
    return arr;
}

C++

int partition(vector<int>& arr, int lo, int hi) {
    int pivot = arr[hi], idx = lo;
    for (int i = lo; i < hi; i++)
        if (arr[i] < pivot) swap(arr[i], arr[idx++]);
    swap(arr[idx], arr[hi]);
    return idx;
}

void quickSort(vector<int>& arr, int lo, int hi) {
    if (lo >= hi) return;
    int p = partition(arr, lo, hi);
    quickSort(arr, lo, p - 1);
    quickSort(arr, p + 1, hi);
}

Time: O(n log n) avg, O(n²) worst | Space: O(log n) | Stable: No


Cyclic Sort

For arrays containing elements 1 to N (or 0 to N-1), place each element at its correct index in one pass.

Array: [3, 1, 5, 4, 2] (elements 1-5)

i=0: arr[0]=3, should be at index 2 → swap → [5, 1, 3, 4, 2]
i=0: arr[0]=5, should be at index 4 → swap → [2, 1, 3, 4, 5]
i=0: arr[0]=2, should be at index 1 → swap → [1, 2, 3, 4, 5]
i=0: arr[0]=1, correct! → move on
i=1-4: all correct
Result: [1, 2, 3, 4, 5] ✓
function cyclicSort(arr) {
    let i = 0;
    while (i < arr.length) {
        const correctIdx = arr[i] - 1;
        if (arr[i] !== arr[correctIdx]) {
            [arr[i], arr[correctIdx]] = [arr[correctIdx], arr[i]];
        } else {
            i++;
        }
    }
    return arr;
}
void cyclicSort(vector<int>& arr) {
    int i = 0;
    while (i < arr.size()) {
        int correct = arr[i] - 1;
        if (arr[i] != arr[correct]) swap(arr[i], arr[correct]);
        else i++;
    }
}

Time: O(n) | Space: O(1)

Applications of cyclic sort

  • Find missing number (1 to N)
  • Find duplicate number
  • Find all missing numbers
  • Find the first missing positive

Sorting algorithms comparison

AlgorithmBestAverageWorstSpaceStable
SelectionO(n²)O(n²)O(n²)O(1)No
InsertionO(n)O(n²)O(n²)O(1)Yes
MergeO(n log n)O(n log n)O(n log n)O(n)Yes
QuickO(n log n)O(n log n)O(n²)O(log n)No
CyclicO(n)O(n)O(n)O(1)No*
HeapO(n log n)O(n log n)O(n log n)O(1)No
CountingO(n+k)O(n+k)O(n+k)O(k)Yes

*Cyclic sort only works for 1-N range


Scenario bank (merge, quick, cyclic sort)

7.9b-001 — Trace merge sort on specific array (v1)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-002 — Count merge sort inversions (v2)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-003 — Implement bottom-up merge sort (v3)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-004 — Quick sort with random pivot (v4)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-005 — Three-way partition (Dutch flag + quicksort) (v5)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-006 — Find kth largest with quickselect (v6)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-007 — Implement cyclic sort for 0-indexed (v7)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-008 — Find missing number using cyclic sort (v8)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-009 — Find all duplicates using cyclic sort (v9)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-010 — Compare stable vs unstable sort results (v10)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-011 — External merge sort for large files (v11)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-012 — Quick sort worst case analysis (v12)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-013 — Merge sort for linked list (v13)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-014 — Hybrid sort (insertion + merge) (v14)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-015 — Counting sort implementation (v15)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-016 — Radix sort implementation (v16)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-017 — Bucket sort implementation (v17)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-018 — Sort colors (Dutch national flag) (v18)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-019 — Sort by frequency (v19)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-020 — Custom comparator sorting (v20)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-021 — Trace merge sort on specific array (v21)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-022 — Count merge sort inversions (v22)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-023 — Implement bottom-up merge sort (v23)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-024 — Quick sort with random pivot (v24)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-025 — Three-way partition (Dutch flag + quicksort) (v25)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-026 — Find kth largest with quickselect (v26)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-027 — Implement cyclic sort for 0-indexed (v27)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-028 — Find missing number using cyclic sort (v28)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-029 — Find all duplicates using cyclic sort (v29)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-030 — Compare stable vs unstable sort results (v30)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-031 — External merge sort for large files (v31)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-032 — Quick sort worst case analysis (v32)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-033 — Merge sort for linked list (v33)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-034 — Hybrid sort (insertion + merge) (v34)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-035 — Counting sort implementation (v35)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-036 — Radix sort implementation (v36)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-037 — Bucket sort implementation (v37)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-038 — Sort colors (Dutch national flag) (v38)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-039 — Sort by frequency (v39)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-040 — Custom comparator sorting (v40)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-041 — Trace merge sort on specific array (v41)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-042 — Count merge sort inversions (v42)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-043 — Implement bottom-up merge sort (v43)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-044 — Quick sort with random pivot (v44)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-045 — Three-way partition (Dutch flag + quicksort) (v45)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-046 — Find kth largest with quickselect (v46)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-047 — Implement cyclic sort for 0-indexed (v47)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-048 — Find missing number using cyclic sort (v48)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-049 — Find all duplicates using cyclic sort (v49)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-050 — Compare stable vs unstable sort results (v50)

  • Level: Beginner
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-051 — External merge sort for large files (v51)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-052 — Quick sort worst case analysis (v52)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-053 — Merge sort for linked list (v53)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-054 — Hybrid sort (insertion + merge) (v54)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-055 — Counting sort implementation (v55)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-056 — Radix sort implementation (v56)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-057 — Bucket sort implementation (v57)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-058 — Sort colors (Dutch national flag) (v58)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-059 — Sort by frequency (v59)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-060 — Custom comparator sorting (v60)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-061 — Trace merge sort on specific array (v61)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-062 — Count merge sort inversions (v62)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-063 — Implement bottom-up merge sort (v63)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-064 — Quick sort with random pivot (v64)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-065 — Three-way partition (Dutch flag + quicksort) (v65)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-066 — Find kth largest with quickselect (v66)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-067 — Implement cyclic sort for 0-indexed (v67)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-068 — Find missing number using cyclic sort (v68)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-069 — Find all duplicates using cyclic sort (v69)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-070 — Compare stable vs unstable sort results (v70)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-071 — External merge sort for large files (v71)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-072 — Quick sort worst case analysis (v72)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-073 — Merge sort for linked list (v73)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-074 — Hybrid sort (insertion + merge) (v74)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-075 — Counting sort implementation (v75)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-076 — Radix sort implementation (v76)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-077 — Bucket sort implementation (v77)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-078 — Sort colors (Dutch national flag) (v78)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-079 — Sort by frequency (v79)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-080 — Custom comparator sorting (v80)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-081 — Trace merge sort on specific array (v81)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-082 — Count merge sort inversions (v82)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-083 — Implement bottom-up merge sort (v83)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-084 — Quick sort with random pivot (v84)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-085 — Three-way partition (Dutch flag + quicksort) (v85)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-086 — Find kth largest with quickselect (v86)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-087 — Implement cyclic sort for 0-indexed (v87)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-088 — Find missing number using cyclic sort (v88)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-089 — Find all duplicates using cyclic sort (v89)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-090 — Compare stable vs unstable sort results (v90)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-091 — External merge sort for large files (v91)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-092 — Quick sort worst case analysis (v92)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-093 — Merge sort for linked list (v93)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-094 — Hybrid sort (insertion + merge) (v94)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-095 — Counting sort implementation (v95)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-096 — Radix sort implementation (v96)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-097 — Bucket sort implementation (v97)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-098 — Sort colors (Dutch national flag) (v98)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-099 — Sort by frequency (v99)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-100 — Custom comparator sorting (v100)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-101 — Trace merge sort on specific array (v101)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-102 — Count merge sort inversions (v102)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-103 — Implement bottom-up merge sort (v103)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-104 — Quick sort with random pivot (v104)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-105 — Three-way partition (Dutch flag + quicksort) (v105)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-106 — Find kth largest with quickselect (v106)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-107 — Implement cyclic sort for 0-indexed (v107)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-108 — Find missing number using cyclic sort (v108)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-109 — Find all duplicates using cyclic sort (v109)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-110 — Compare stable vs unstable sort results (v110)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-111 — External merge sort for large files (v111)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-112 — Quick sort worst case analysis (v112)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-113 — Merge sort for linked list (v113)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-114 — Hybrid sort (insertion + merge) (v114)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-115 — Counting sort implementation (v115)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-116 — Radix sort implementation (v116)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-117 — Bucket sort implementation (v117)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-118 — Sort colors (Dutch national flag) (v118)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-119 — Sort by frequency (v119)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-120 — Custom comparator sorting (v120)

  • Level: Intermediate
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-121 — Trace merge sort on specific array (v121)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-122 — Count merge sort inversions (v122)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-123 — Implement bottom-up merge sort (v123)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-124 — Quick sort with random pivot (v124)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-125 — Three-way partition (Dutch flag + quicksort) (v125)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-126 — Find kth largest with quickselect (v126)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-127 — Implement cyclic sort for 0-indexed (v127)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-128 — Find missing number using cyclic sort (v128)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-129 — Find all duplicates using cyclic sort (v129)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-130 — Compare stable vs unstable sort results (v130)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-131 — External merge sort for large files (v131)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-132 — Quick sort worst case analysis (v132)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-133 — Merge sort for linked list (v133)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-134 — Hybrid sort (insertion + merge) (v134)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-135 — Counting sort implementation (v135)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-136 — Radix sort implementation (v136)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-137 — Bucket sort implementation (v137)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-138 — Sort colors (Dutch national flag) (v138)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-139 — Sort by frequency (v139)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-140 — Custom comparator sorting (v140)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-141 — Trace merge sort on specific array (v141)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-142 — Count merge sort inversions (v142)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-143 — Implement bottom-up merge sort (v143)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-144 — Quick sort with random pivot (v144)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-145 — Three-way partition (Dutch flag + quicksort) (v145)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-146 — Find kth largest with quickselect (v146)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-147 — Implement cyclic sort for 0-indexed (v147)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-148 — Find missing number using cyclic sort (v148)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-149 — Find all duplicates using cyclic sort (v149)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-150 — Compare stable vs unstable sort results (v150)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-151 — External merge sort for large files (v151)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-152 — Quick sort worst case analysis (v152)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-153 — Merge sort for linked list (v153)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-154 — Hybrid sort (insertion + merge) (v154)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-155 — Counting sort implementation (v155)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-156 — Radix sort implementation (v156)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-157 — Bucket sort implementation (v157)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-158 — Sort colors (Dutch national flag) (v158)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-159 — Sort by frequency (v159)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-160 — Custom comparator sorting (v160)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-161 — Trace merge sort on specific array (v161)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-162 — Count merge sort inversions (v162)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-163 — Implement bottom-up merge sort (v163)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-164 — Quick sort with random pivot (v164)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-165 — Three-way partition (Dutch flag + quicksort) (v165)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-166 — Find kth largest with quickselect (v166)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-167 — Implement cyclic sort for 0-indexed (v167)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-168 — Find missing number using cyclic sort (v168)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-169 — Find all duplicates using cyclic sort (v169)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.9b-170 — Compare stable vs unstable sort results (v170)

  • Level: Advanced
  • Approach: Apply appropriate technique.
  • Time: Analyze.
  • Space: Identify.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.