The basic/ directory contains educational tutorials for fundamental algorithms with detailed explanations and multi-language implementations. This module covers 8 sorting algorithms and binary search techniques, providing working code examples in 10+ programming languages. Each algorithm includes complexity analysis, algorithmic intuition, and reusable templates.
This content is designed for learning core algorithmic concepts. For applications of these algorithms in solving LeetCode problems, see problem collections in page 3 and solution patterns in page 4.
The basic algorithms module is organized into two main categories: sorting algorithms and searching algorithms. Each algorithm is contained in its own directory with README documentation and implementations across multiple languages.
Directory Pattern for Each Algorithm
Each algorithm directory follows a consistent structure:
README.md - Algorithm explanation with complexity analysis and multi-language code examples (Chinese)<AlgorithmName>.<ext> or Solution.<ext> or Main.<ext>.py), Java (.java), C++ (.cpp), Go (.go), Rust (.rs), JavaScript (.js), C# (.cs)Sources: basic/README.md1-32 basic/README_EN.md1-32 basic/summary.md1-13
The module provides detailed implementations and explanations for 8 classic sorting algorithms. Each tutorial includes algorithmic intuition, step-by-step explanations, and runnable code examples.
| Algorithm | Best Time | Average Time | Worst Time | Space | Stable | Directory |
|---|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | basic/sorting/BubbleSort/ |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | basic/sorting/InsertionSort/ |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No | basic/sorting/SelectionSort/ |
| Shell Sort | O(n log n) | O(n^1.3) | O(n²) | O(1) | No | basic/sorting/ShellSort/ |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | basic/sorting/MergeSort/ |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No | basic/sorting/QuickSort/ |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | basic/sorting/HeapSort/ |
| Counting Sort | O(n+k) | O(n+k) | O(n+k) | O(n+k) | Yes | basic/sorting/CountingSort/ |
k = range of input values for Counting Sort
Sources: basic/sorting/BubbleSort/README.md1-6 basic/sorting/InsertionSort/README.md1-14 basic/sorting/SelectionSort/README.md1-3 basic/sorting/ShellSort/README.md1-8 basic/sorting/MergeSort/README.md1-5 basic/sorting/QuickSort/README.md1-3 basic/sorting/HeapSort/README.md1-36 basic/sorting/CountingSort/README.md1-16
Bubble Sort uses a hasChange flag optimization to detect when the array becomes sorted, allowing early termination. Adjacent elements are compared and swapped if out of order.
Algorithm Pattern:
The key optimization is early termination when no swaps occur in a pass, indicating the array is already sorted.
Available Implementations:
Sources: basic/sorting/BubbleSort/README.md1-234 basic/sorting/BubbleSort/BubbleSort.java1-29 basic/sorting/BubbleSort/BubbleSort.cpp1-25
Merge Sort implements divide-and-conquer by recursively splitting arrays and merging sorted halves. The tutorial provides a reusable template.
Algorithm Template (Java):
The merge phase uses a temporary array tmp[] with three steps: merge both halves in order, copy remaining left elements, copy remaining right elements.
Available Implementations:
Sources: basic/sorting/MergeSort/README.md1-374 basic/sorting/MergeSort/Main.java1-43 basic/sorting/MergeSort/Main.cpp1-34
Quick Sort partitions an array around a pivot element using two-pointer scanning. The tutorial emphasizes the partitioning technique.
Algorithm Template (Java):
The partitioning uses two pointers i and j scanning from opposite ends, swapping elements to maintain: [left..j] <= pivot, [j+1..right] >= pivot.
Available Implementations:
Sources: basic/sorting/QuickSort/README.md1-331 basic/sorting/QuickSort/Main.rs1-48
Heap Sort uses a binary heap data structure with 1-indexed array representation. The tutorial provides reusable heap operation templates.
Heap Structure Convention:
h[] - array storing heap values
h[1] - heap root (1-indexed)
h[2*x] - left child of node x
h[2*x+1] - right child of node x
h[x/2] - parent of node x
Core Operations:
The down(int u) operation (heapify-down):
The up(int u) operation (heapify-up):
O(n) Heap Building:
Available Implementations:
Sources: basic/sorting/HeapSort/README.md1-501 basic/sorting/HeapSort/Main.go1-51 basic/sorting/HeapSort/Main.rs1-40
The remaining sorting algorithms follow similar documentation patterns with detailed explanations and multi-language code examples.
Insertion Sort - Maintains sorted and unsorted regions, inserting each element into its correct position:
nums[i] into sorted nums[0..i-1]Selection Sort - Repeatedly selects minimum element from unsorted region and moves to end of sorted region:
Shell Sort - Improved insertion sort using gap sequences to make array partially ordered:
n/2, decreases by half each iteration until gap = 1Counting Sort - Non-comparison integer sort using frequency counting:
Sources: basic/sorting/InsertionSort/README.md1-214 basic/sorting/SelectionSort/README.md1-216 basic/sorting/ShellSort/README.md1-125 basic/sorting/CountingSort/README.md1-17
The binary search tutorial in basic/searching/BinarySearch/README.md emphasizes a key insight: the essence of binary search is finding "boundaries", not just "monotonicity". Any property that divides an interval into two parts enables binary search to locate the boundary point.
Title: Binary Search Conceptual Model
The tutorial provides two reusable templates from basic/searching/BinarySearch/README.md8-43 The key difference is which boundary (left or right) you're finding.
Use when check(mid) == true means search left (right = mid):
Critical detail: When using right = mid, calculate mid as (left + right) >> 1 (no +1).
Use when check(mid) == true means search right (left = mid):
Critical detail: When using left = mid, must calculate mid as (left + right + 1) >> 1 (with +1) to avoid infinite loops.
The tutorial at basic/searching/BinarySearch/README.md45-54 provides a systematic 4-step approach:
Title: Binary Search Implementation Flowchart
Invariant Properties:
[left, right] during searchleft == rightleft or right satisfies the original constraintsThe tutorial at basic/searching/BinarySearch/README.md56-63 references LeetCode problems demonstrating these templates:
| Problem | Template | Boundary Property |
|---|---|---|
| Find First and Last Position | Both 1 & 2 | Left boundary: first occurrence; Right boundary: last occurrence |
| Sqrt(x) | Template 2 | Find largest x where x² ≤ n |
| Find Peak Element | Custom | Find local maximum using gradient |
| First Bad Version | Template 1 | Find first true in boolean array |
| Fixed Point | Template 1 | Find minimum index where arr[i] == i |
Sources: basic/searching/BinarySearch/README.md1-63 basic/searching/BinarySearch/README_EN.md1-63
Each algorithm tutorial demonstrates the same logic across multiple programming languages, allowing learners to see how algorithmic concepts translate into different language idioms.
The following table shows which programming languages have implementations for each algorithm:
| Algorithm | Python | Java | C++ | Go | Rust | JavaScript | C# | Total |
|---|---|---|---|---|---|---|---|---|
| Bubble | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | 7 |
| Insertion | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | 7 |
| Selection | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | 7 |
| Shell | - | ✓ | - | ✓ | ✓ | ✓ | - | 4 |
| Merge | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | - | 6 |
| Quick | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | - | 6 |
| Heap | ✓ | ✓ | - | ✓ | ✓ | - | - | 4 |
| Counting | - | ✓ | - | ✓ | - | - | - | 2 |
| Binary Search | - | Template | - | - | - | - | - | 1 |
Binary Search provides Java template code in documentation
All implementations follow consistent patterns across languages:
Input/Output Handling:
n then n integersFunction Signatures:
void functions that mutate input arrays in-place&mut Vec<i32> for mutationArray Indexing:
h[1] is root, h[2*x] is left child)Code Formatting:
IndentWidth: 4, .clang-format154-159 defines brace stylesSources: basic/sorting/MergeSort/Main.java1-43 basic/sorting/HeapSort/README.md1-36 .clang-format1-277
The basic algorithms module is accessible through multiple entry points in the repository:
From the Chinese README:
From the English README:
The module is integrated into the documentation website via:
https://leetcode.doocs.org → 基础算法通关https://leetcode.doocs.org/en → Basic AlgorithmsTwo index pages provide algorithm listings:
Sources: README.md37-56 README_EN.md36-54 basic/README.md1-32 basic/README_EN.md1-32 basic/summary.md1-13
The basic algorithms module follows the repository's standard practices:
All code files are subject to automated formatting:
ColumnLimit: 100 (Java), ColumnLimit: 0 (C++)IndentWidth: 4AllowShortFunctionsOnASingleLine: AllThough primarily educational, the basic algorithms code is validated through:
Sources: .clang-format1-277 README.md14-16 README_EN.md14-16
Refresh this wiki
This wiki was recently refreshed. Please wait 2 days to refresh again.