diff --git a/algorithms/foundations/data-structures/doubly-linked-list.md b/algorithms/foundations/data-structures/doubly-linked-list.md new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/foundations/data-structures/graph.md b/algorithms/foundations/data-structures/graph.md new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/foundations/data-structures/hash-table.md b/algorithms/foundations/data-structures/hash-table.md new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/foundations/data-structures/linked-list.md b/algorithms/foundations/data-structures/linked-list.md new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/foundations/data-structures/map.md b/algorithms/foundations/data-structures/map.md new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/foundations/data-structures/priority-queue.md b/algorithms/foundations/data-structures/priority-queue.md new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/foundations/data-structures/set.md b/algorithms/foundations/data-structures/set.md new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/foundations/data-structures/tree.md b/algorithms/foundations/data-structures/tree.md new file mode 100644 index 0000000..e69de29 diff --git a/algorithms/foundations/sorting/README.md b/algorithms/foundations/sorting/README.md index e69de29..9db91bf 100644 --- a/algorithms/foundations/sorting/README.md +++ b/algorithms/foundations/sorting/README.md @@ -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 diff --git a/algorithms/foundations/sorting/merge-sort.md b/algorithms/foundations/sorting/merge-sort.md index e69de29..fab1b61 100644 --- a/algorithms/foundations/sorting/merge-sort.md +++ b/algorithms/foundations/sorting/merge-sort.md @@ -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 \ No newline at end of file