All Topics

Sorting & Searching

Classic sorting algorithms, custom comparators, and advanced search techniques

0/32 solved

Merge Sort & Divide-and-Conquer

Divide array in half, sort recursively, merge — stable O(n log n) sort

O(n log n) O(n)

Approach

Divide array into halves, recursively sort each half, then merge the two sorted halves into one. During merge, perform any counting/aggregation needed at the combination step.

How to Recognize

1

Need to count inversions during sorting

2

Merge sort is the basis for many divide-and-conquer problems

3

Phrases like 'count inversions', 'reverse pairs', 'merge sort'

4

Need a stable sort that preserves relative order of equal elements

Pro Tips

Count inversions = count pairs (i,j) where i < j but a[i] > a[j]

Merge step is where counting happens — when an element from right comes before left

Can be used for custom sorting when comparison-based approaches are needed

8

Total

0

Easy

4

Medium

4

Hard

Time

O(n log n)

Space

O(n)