Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
43 changes: 43 additions & 0 deletions algorithms/foundations/sorting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Sorting Algorithms

## Overview
Sorting algorithms reorder elements of a collection according to a defined order
(e.g. ascending or descending). They are fundamental building blocks used in
searching, optimization, and data processing.

Different sorting algorithms make different trade-offs in terms of:
- time complexity
- space usage
- stability
- adaptability to input characteristics

---

## Classification

### Comparison-based
- Merge Sort
- Quick Sort
- Heap Sort

### Non-comparison-based
- Counting Sort
- Radix Sort
- Bucket Sort

---

## When to use which

| Algorithm | Time (avg) | Space | Stable | Notes |
|---------------|-----------|-------|--------|------|
| Merge Sort | O(n log n) | O(n) | Yes | Predictable, good for linked lists |
| Quick Sort | O(n log n) | O(log n) | No | Fast in practice, bad worst-case |
| Heap Sort | O(n log n) | O(1) | No | In-place, not stable |
| Counting Sort | O(n + k) | O(k) | Yes | Only for small integer ranges |

---

## Related techniques
- Divide and Conquer
- Heap data structure
72 changes: 72 additions & 0 deletions algorithms/foundations/sorting/merge-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Merge Sort

## Problem
Given a collection of elements, sort them in non-decreasing order.

Merge Sort is a **comparison-based, divide-and-conquer sorting algorithm**.

---

## High-level idea
The algorithm recursively divides the collection into two halves until subarrays
of size one are reached. These subarrays are then merged back together in sorted
order.

The key insight is that **merging two already sorted arrays can be done efficiently**.

---

## Algorithm steps
1. Divide the array into two halves.
2. Recursively apply Merge Sort to each half.
3. Merge the two sorted halves into a single sorted array.

---

## Correctness argument
Merge Sort is correct by induction on the size of the array.

**Base case:**
An array of size 0 or 1 is trivially sorted.

**Inductive step:**
Assume Merge Sort correctly sorts arrays of size less than `n`.
For an array of size `n`, the algorithm:
- divides it into two smaller arrays,
- recursively sorts each one (by the inductive hypothesis),
- merges the two sorted arrays into a fully sorted array.

Since the merge step preserves order, the final array is sorted.

---

## Complexity
- **Time complexity:** `O(n log n)` in all cases
- **Space complexity:** `O(n)` due to auxiliary arrays

---

## Properties
- Stable: ✅
- In-place: ❌
- Comparison-based: ✅

---

## When to use
- When predictable performance is required
- When stability matters
- When working with linked lists or external sorting

---

## When NOT to use
- When memory usage must be minimal
- When in-place sorting is required

---

## Related concepts
- Divide and Conquer
- Recursion
- External sorting