{"id":23024602,"url":"https://github.com/codejsha/algorithm-examples","last_synced_at":"2025-04-02T19:41:24.099Z","repository":{"id":43346479,"uuid":"510058322","full_name":"codejsha/algorithm-examples","owner":"codejsha","description":"Examples of algorithms, data structures, and problem-solving for practical applications","archived":false,"fork":false,"pushed_at":"2024-12-07T02:06:08.000Z","size":1452,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-08T10:20:55.863Z","etag":null,"topics":["algorithms","data-structures","problem-solving"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codejsha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"codejsha"}},"created_at":"2022-07-03T15:14:50.000Z","updated_at":"2024-12-06T11:45:43.000Z","dependencies_parsed_at":"2024-02-25T04:27:34.719Z","dependency_job_id":"58e4d3a5-f1ae-470a-b8c0-46011da66305","html_url":"https://github.com/codejsha/algorithm-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codejsha%2Falgorithm-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codejsha%2Falgorithm-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codejsha%2Falgorithm-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codejsha%2Falgorithm-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codejsha","download_url":"https://codeload.github.com/codejsha/algorithm-examples/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246882948,"owners_count":20849346,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithms","data-structures","problem-solving"],"created_at":"2024-12-15T13:19:41.140Z","updated_at":"2025-04-02T19:41:24.052Z","avatar_url":"https://github.com/codejsha.png","language":"C++","readme":"# Algorithm Examples\n\n\u003c!-- markdownlint-disable MD033 MD036 --\u003e\n\n![C++ CMake Test](https://github.com/codejsha/algorithm-examples/actions/workflows/cmake.yml/badge.svg) ![Python Poetry Test](https://github.com/codejsha/algorithm-examples/actions/workflows/poetry.yml/badge.svg) ![Go Test](https://github.com/codejsha/algorithm-examples/actions/workflows/go.yml/badge.svg) ![Java Gradle Test](https://github.com/codejsha/algorithm-examples/actions/workflows/gradle.yml/badge.svg)\n\n[English](README.md) | [Korean](README_ko-KR.md)\n\nThis repository provides examples of algorithms, data structures, and problem-solving approaches for practical applications. These examples are implemented in C++, Python, Go, and Java, with each language utilizing its own test framework. Running the tests allows you to execute methods or functions on the underlying algorithmic logic.\n\n## Project Environments\n\nEach project is configured in specific environments, as described below:\n\n| Language | Version | Build                                | Packages                                                                                                                                                                                                              | Remarks                                     |\n| -------- | ------- | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |\n| C++      | C++20   | [CMake](https://cmake.org/)          | [GNU Scientific Library (GSL)](https://www.gnu.org/software/gsl/), [Google Test](https://google.github.io/googletest/), [Google Benchmark](https://github.com/google/benchmark), [fmt](https://github.com/fmtlib/fmt) | [vcpkg](https://github.com/microsoft/vcpkg) |\n| Python   | 3.12    | [Poetry](https://python-poetry.org/) | [NumPy](https://numpy.org/), [SciPy](https://www.scipy.org/), [NetworkX](https://networkx.org/), [pytest](https://docs.pytest.org/), [pytest-benchmark](https://pytest-benchmark.readthedocs.io/en/latest/)           |                                             |\n| Go       | 1.22    | [Go](https://pkg.go.dev/cmd/go)      | [Gonum](https://github.com/gonum/gonum), [Testify](https://github.com/stretchr/testify)                                                                                                                               |                                             |\n| Java     | 21      | [Gradle](https://gradle.org/)        | [Google Guava](https://github.com/google/guava), [JUnit](https://junit.org/), [Java Microbenchmark Harness (JMH)](https://github.com/openjdk/jmh)                                                                     |                                             |\n\n## Table of Contents\n\n- [Project Environments](#project-environments)\n- [Table of Contents](#table-of-contents)\n- [Data structures](#data-structures)\n  - [:train: Array](#train-array)\n  - [:chart\\_with\\_upwards\\_trend: Graph](#chart_with_upwards_trend-graph)\n  - [:key: Hash table](#key-hash-table)\n  - [:rocket: Heap](#rocket-heap)\n  - [:paperclips: Linked list](#paperclips-linked-list)\n  - [:walking: Queue](#walking-queue)\n  - [:hamburger: Stack](#hamburger-stack)\n  - [:deciduous\\_tree: Tree](#deciduous_tree-tree)\n- [Topics](#topics)\n  - [:jigsaw: Dynamic programming (DP)](#jigsaw-dynamic-programming-dp)\n  - [:clock9: Greedy](#clock9-greedy)\n  - [:triangular\\_ruler: Mathematics](#triangular_ruler-mathematics)\n  - [:1234: Primitive type](#1234-primitive-type)\n  - [:mag: Search](#mag-search)\n  - [:abc: Sort](#abc-sort)\n  - [:page\\_facing\\_up: String](#page_facing_up-string)\n- [References](#references)\n\n## Data structures\n\n### :train: Array\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\nauto v = std::vector{1, 2, 3, 4, 5};\nauto sub_v = std::vector\u003cint\u003e{v.begin(), v.end() - 1};\nauto arr = std::array{1, 2, 3, 4, 5};\nauto sub_arr = std::array{arr.begin(), arr.end() - 1};\n\n// algorithm\nstd::ranges::any_of(v, [](auto x) { return x % 2 == 0; })\nstd::ranges::all_of(v, [](auto x) { return x % 2 == 0; })\nstd::ranges::none_of(v, [](auto x) { return x % 2 == 0; })\nstd::ranges::for_each(v, [](auto x) { std::cout \u003c\u003c x \u003c\u003c \" \"; }).fun\nstd::ranges::count(v, 42)\nstd::ranges::count_if(v, [](auto x) { return x % 2 == 0; })\nstd::ranges::reverse(v)\nstd::ranges::rotate(v, v.end() - k)\nstd::ranges::sort(v)\nstd::ranges::min_element(v)\nstd::ranges::max_element(v)\nstd::ranges::minmax_element(v)\n```\n\n**Python declaration/functions**\n\n```python\n# list\nnumber_list: list[int] = [1, 2, 3, 4, 5]\nappend(6), insert(3, 3), remove(4), pop(), pop(0), index(3), count(3), clear(), extend([6, 7, 8])\nnumber_list.reverse()   # in-place\nreversed(number_list)   # return an iterator\nnumber_list.sort()      # in-place\nsorted(number_list)     # return a new list(copy)\ndel(number_list[0])     # delete the first element\ndel(number_list[0:2])   # remove the slice\nbisect.bisect_left(number_list, 3), bisect.bisect_right(number_list, 3), bisect.bisect(number_list, 3)\nbisect.insort_left(number_list, 3), bisect.insort_right(number_list, 3), bisect.insort(number_list, 3)\n\n# tuple\nsample_tuple: tuple[int] = (1, 2, 3, 4, 5)\nindex(3), count(3)\n\n### list comprehension\n# single level of loop\neven_list = [x for x in number_list if x % 2 == 0]\n# two levels of loop\nsample_list = [[1, 2, 3], [4, 5, 6]]\nsquare_list = [[n ** 2 for n in row] for row in sample_list]\n# multi levels of loop\nlist_a: list[int] = [1, 3, 5]\nlist_b: list[str] = ['a', 'b']\nset_ab: set[tuple[int, str]] = {(a, b) for a in list_a for b in list_b}\n# 2d array to 1d array\nlist_2d = [['a', 'b', 'c'], ['d', 'e', 'f']]\nlist_1d: list[str] = [ch for row in list_2d for ch in row]\n# any/all\nany(x % 2 == 0 for x in number_list)\nall(x % 2 == 0 for x in number_list)\n```\n\n**Java declaration/methods**\n\n```java\nint[] arr = new int[]{1, 2, 3, 4, 5};   // array\nint[][] matrix = new int[m][n];         // 2d array (m by n matrix)\n```\n\n```java\nimport java.util.*;\n\n// Arrays\nbinarySearch(arr, 3), equals(arr, another_arr), copyOf(arr, arr.length), copyOfRange(arr, from, to),\nsort(arr), sort(arr, from, to), fill(arr, 42), fill(arr, from, to, 42),\n// Arrays.stream()\nanyMatch(x -\u003e x % 2 == 0), allMatch(x -\u003e x % 2 == 0), noneMatch(x -\u003e x % 2 == 0),\ncount(), sum(), min(), max(), average(), map(x -\u003e x * 2).toArray(), filter(x -\u003e x % 2 == 0).count()\n\n// Collections\nsort(list), binarySearch(list, 3), min(list), max(list), swap(list, 0, 1), replaceAll(list, 1, 2),\nfrequency(list, 1), reverse(list), rotate(list, 1), shuffle(list), unmodifiableList(list)\n\n// list\nvar list = Arrays.asList(boxedArray);\nArrays.stream(arr).boxed().collect(Collectors.toList())\nsort(), sort(Comparator.naturalOrder()), sort(Comparator.reverseOrder())\n\n// string\nString.join(\", \", arr)                          // array to string\nstr.split(\"\"), str.split(\" \"), str.split(\", \")  // string to array\nstr.toCharArray()                               // string to char array\nstr.chars().toArray()                           // string to int array\n\n// boxing\nvar arr1 = Arrays.stream(arr).boxed().toArray(Integer[]::new);                     // int[] to Integer[]\nvar arr2 = Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new);    // int[] to String[]\nvar arr3 = Arrays.stream(arr).boxed().collect(Collectors.toList());                // int[] to List\u003cInteger\u003e\n\n// integer sequence\nvar arr = IntStream.range(0, n).toArray();          // range to int array in [0, n)\nvar arr = IntStream.rangeClosed(1, n).toArray();    // range to int array in [1, n]\nvar list = IntStream.range(0, n).boxed().toList();  // range to list\n\nvar list = List.of(arr);        // array to list\nvar list = Arrays.asList(arr);  // array to list\nvar arr = strList.toArray(String[]::new);  // List\u003cString\u003e to String[]\nvar arr = intList.stream().mapToInt(Integer::intValue).toArray();  // List\u003cInteger\u003e to int[]\nvar list = Arrays.stream(arr).boxed().sorted().collect(Collectors.toCollection(ArrayList::new));    // array to sorted list\n\n// guava\nimport com.google.common.collect.*;\nList\u003cString\u003e list = Lists.newArrayList();\nList\u003cString\u003e list = Lists.asList(boxedArray);\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Examples**\n\n- Advancing through an array, EPI#5.4: [cpp](/cpp-algorithm/src/array/advancing_through.h) | Advance through the array to the last index.\n- Arbitrary precision operation\n  - increment an arbitrary-precision integer, EPI#5.2: [cpp](/cpp-algorithm/src/array/arbitrary_precision_integer.h)(`PlusOne`) | Add one to the number represented by the vector.\n  - add two arbitrary-precision integers: [cpp](/cpp-algorithm/src/array/arbitrary_precision_integer.h)(`StringAddition`) | Add two numbers represented by strings.\n  - multiply two arbitrary-precision integers, EPI#5.3: [cpp](/cpp-algorithm/src/array/arbitrary_precision_integer.h)(`Multiply`) | Multiply two numbers represented by vectors.\n- Delete duplicates from a sorted array, EPI#5.5: [cpp](/cpp-algorithm/src/array/delete_element.h)(`DeleteDuplicates`, `DeleteDuplicateElements`) | Delete duplicate elements in the array.\n- Delete specific elements from a sorted array: [cpp](/cpp-algorithm/src/array/delete_element.h)(`DeleteSpecificElements`) | Delete specific elements in the array.\n- Dutch national flags problem, EPI#5.1: [cpp](/cpp-algorithm/src/array/dutch_national_flag.h)\n- Enumerate prime numbers, EPI#5.9: [cpp](/cpp-algorithm/src/array/enumerate_prime_number.h) | Enumerate prime numbers in the range.\n- Order elements in an array by even and odd: [cpp](/cpp-algorithm/src/array/order_element.h)(`EvenOdd`) | Order even and odd numbers in the array.\n- Order elements in an array by specified order, EPI#5.8: [cpp](/cpp-algorithm/src/array/order_element.h)(`Rearrange`) | Rearrange arrays to have a specific order.\n- Random data sampling: Select the k elements randomly from the array with uniform probability.\n  - offline, EPI#5.12: [cpp](/cpp-algorithm/src/array/random_data_sampling.h)(`OfflineRandomSampling`) | Design an algorithm to return a random subset of $k$ elements from an array.\n  - online, EPI#5.13: [cpp](/cpp-algorithm/src/array/random_data_sampling.h)(`OnlineRandomSampling`) | Design an algorithm that reads data and creates a random subset of size $k$.\n  - compute permutation, EPI#5.14: [cpp](/cpp-algorithm/src/array/random_data_sampling.h)(`ComputeRandomPermutation`) | Compute permutation of the array generated by random sampling.\n- Replace elements\n  - replace and remove: [cpp](/cpp-algorithm/src/array/replace_element.h)(`ReplaceAndRemoveString1`, `ReplaceAndRemoveString2`) | Replace element and remove element in the array.\n  - telex encoding: [cpp](/cpp-algorithm/src/array/replace_element.h)(`TelexEncoding`) | Telex encoding for punctuation marks.\n- Stock trading\n  - buy and sell a stock once, EPI#5.6: [cpp](/cpp-algorithm/src/array/stock_trading.h)(`BuyAndSellStockOnceBruteForce`, `BuyAndSellStockOnce`)\n  - buy and sell a stock twice, EPI#5.7: [cpp](/cpp-algorithm/src/array/stock_trading.h)(`BuyAndSellStockTwice`)\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :chart_with_upwards_trend: Graph\n\n- Shortest path algorithm\n  - Single-source: Bellman-Ford algorithm, Dijkstra's algorithm\n  - Single-pair: A\\* search algorithm\n  - All-pair: Floyd-Warshall algorithm, Johnson's algorithm\n- Minimum spanning tree algorithm: Kruskal's algorithm, Prim's algorithm\n- Maximum flow algorithm: Edmonds-Karp algorithm, Ford-Fulkerson algorithm, Push-relabel algorithm, Maximum bipartite matching\n\n\u003c!-- @formatter:off --\u003e\n\n**Python declaration/functions**\n\n```python\n# networkx\nimport networkx\n\ngraph = networkx.Graph()\n\n# edges\nedges = [(seattle, chicago), (seattle, san_francisco), ...]\ngraph.add_edges_from(edges)\n\n# weighted edges\nweighted_edges = [(seattle, chicago, 1737), (seattle, san_francisco, 678), ...]\ngraph.add_weighted_edges_from(weighted_edges)\n\n# operations\nnetworkx.bfs_layers(graph, \"Boston\")\nnetworkx.minimum_spanning_tree(graph, algorithm=\"kruskal\")\nnetworkx.dijkstra_path(graph, \"Los Angeles\", \"Boston\")\n```\n\n**Java declaration/methods**\n\n```java\n// guava graph\nimport com.google.common.graph.*;\nMutableGraph\u003cInteger\u003e graph = GraphBuilder.undirected().build();\nMutableValueGraph\u003cCity, Distance\u003e roads = ValueGraphBuilder.directed()\n        .incidentEdgeOrder(ElementOrder.stable())\n        .build();\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Graph algorithms**\n\n\u003e NOTE: In the following, the C++ implementations are currently only available on MSVC.\n\n- A\\* search algorithm, CCSP#2.2.5: A single-pair shortest path algorithm. This is a variant of Dijkstra's algorithm using heuristics to try to speed up the search.\n- Bellman-Ford algorithm, CLRS#24.1: [cpp](/cpp-algorithm/src/graph/bellman_ford.h), [java#1](/java-algorithm/src/main/java/com/example/algorithm/graph/BellmanFord1.java), [java#2](/java-algorithm/src/main/java/com/example/algorithm/graph/BellmanFord2.java) | A single source the shortest path algorithm that can handle negative edge weights. It finds the shortest path from a source vertex to all other vertices in a weighted graph.\n\n```txt\nalgorithm BellmanFord(G, source):\n    // Initialize single source\n    for each u in G.V:\n        u.distance = ∞\n        u.parent = NIL\n    source.distance = 0\n\n    for i = 0 to |G.V| - 2:\n        for each edge (u, v) in G.E:\n            // Relaxation\n            if v.distance \u003e u.distance + w(u, v):\n                v.distance = u.distance + w(u, v)\n                v.parent = u\n\n    for each edge (u, v) in G.E:\n        if v.distance \u003e u.distance + w(u, v):\n            return false\n    return true\n```\n\n- Breadth-first search (BFS), CLRS#22.2, CCSP#2.2.4, CCSP#4.3.1: [cpp](/cpp-algorithm/src/graph/breadth_first_search.h), [python(test)](/python-algorithm/algorithm/graph/test/test_breadth_first_search.py), [java#1](/java-algorithm/src/main/java/com/example/algorithm/graph/BreadthFirstSearch1.java), [java#2](/java-algorithm/src/main/java/com/example/algorithm/graph/BreadthFirstSearch2.java) | A search algorithm that traverses a graph layer by layer. Check the shortest path and compute the distance from the source vertex to all other vertices.\n\n```txt\nalgorithm BFS(G, source):\n    for each u in G.V:\n        u.color = WHITE\n        u.distance = ∞\n        u.parent = NIL\n    source.color = GRAY\n    source.distance = 0\n    source.parent = NIL\n    Queue = ∅\n    Queue.enqueue(source)\n\n    while Queue != ∅:\n        u = Queue.dequeue()\n        for each v in G.Adj[u]:\n            if v.color == WHITE:\n                v.color = GRAY\n                v.distance = u.distance + 1\n                v.parent = u\n                Queue.enqueue(v)\n        u.color = BLACK\n```\n\n- Depth-first search (DFS), CLRS#22.3, CCSP#2.2.3: [cpp](/cpp-algorithm/src/graph/depth_first_search.h), [java#1](/java-algorithm/src/main/java/com/example/algorithm/graph/DepthFirstSearch1.java), [java#2](/java-algorithm/src/main/java/com/example/algorithm/graph/DepthFirstSearch2.java), [java#3](/java-algorithm/src/main/java/com/example/algorithm/graph/DepthFirstSearch3.java) | A search algorithm that traverses a graph by exploring as far as possible along each branch before backtracking. Check to exists cycle in a graph.\n\n```txt\nalgorithm DFS(G):\n    for each u in G.V:\n        u.color = WHITE\n        u.parent = NIL\n    time = 0\n    for each u in G.V:\n        if u.color == WHITE:\n            DFS-VISIT(G, u)\n\nalgorithm DFS-VISIT(G, u):\n    time = time + 1               // discovered\n    u.discovered = time\n    u.color = GRAY\n    for each v in G.Adj[u]:\n        if v.color == WHITE:\n            v.parent = u\n            DFS-VISIT(G, v)\n    u.color = BLACK\n    time = time + 1               // finished\n    u.finished = time\n```\n\n- Dijkstra's algorithm, CLRS#24.3, CCSP#4.5.1: [cpp](/cpp-algorithm/src/graph/dijkstra.h), [python(test)](/python-algorithm/algorithm/graph/test/test_dijkstra.py), [java](/java-algorithm/src/main/java/com/example/algorithm/graph/Dijkstra.java) | A single source the shortest path algorithm that handle non-negative edge weights. It finds the shortest path between two vertices in a graph.\n\n```txt\nalgorithm Dijkstra(G, source):\n    // Initialize single source\n    for each u in G.V:\n        u.distance = ∞\n        u.parent = NIL\n    source.distance = 0\n\n    Set = ∅\n    Queue = G.V\n    while Queue != ∅:\n        u = EXTRACT-MIN(Queue)\n        Set = Set ∪ {u}\n        for each v in G.Adj[u]:\n            // Relaxation\n            if v.distance \u003e u.distance + w(u, v):\n                v.distance = u.distance + w(u, v)\n                v.parent = u\n```\n\n- Edmonds-Karp algorithm\n- Floyd-Warshall algorithm, CLRS#25.2: [java](/java-algorithm/src/main/java/com/example/algorithm/graph/FloydWarshall.java) | A all pairs shortest paths algorithm.\n\n```txt\nalgorithm InitializeAdjacencyMatrix(G):\n    d = matrix of size |G.V| × |G.V|\n    for each u in G.V:\n        for each v in G.V:\n            if u == v:\n                d[u][v] = 0\n            else if (u, v) in G.E:\n                d[u][v] = w(u, v)\n            else:\n                d[u][v] = ∞\n    return d\n\nalgorithm FloydWarshall(G):\n    d = InitializeAdjacencyMatrix(G)\n    for k = 0 to |G.V| - 1:\n        for i = 0 to |G.V| - 1:\n            for j = 0 to |G.V| - 1:\n                dᵏ[i, j] = MIN(dᵏ⁻¹[i, j], dᵏ⁻¹[i, k] + dᵏ⁻¹[k, j])\n    return d\n```\n\n- Ford-Fulkerson algorithm, CLRS#26.2\n- Johnson's algorithm, CLRS#25.3: A all pairs shortest paths algorithm. This is a combination of Dijkstra's algorithm and the Bellman-Ford algorithm. It may be faster than Floyd–Warshall on sparse graphs.\n- Kruskal's algorithm, CLRS#23.2, CLRS#21.1: [java](/java-algorithm/src/main/java/com/example/algorithm/graph/Kruskal.java) | A minimum spanning tree algorithm. It finds a minimum spanning forest of an undirected edge-weighted graph. The algorithm uses path compression (FIND-SET) and union by rank (UNION) to improve the performance.\n\n```txt\nalgorithm Kruskal(G, w):\n    Set = ∅\n    for each v in G.V:\n        MAKE-SET(v)                                 // initialize vertice\n    for each edge (u, v) in G.E ordered by w(u, v), increasing:\n        if FIND-SET(u) != FIND-SET(v):\n            Set = Set ∪ {(u, v)}\n            UNION(u, v)                             // combine trees\n    return Set\n```\n\n- Maximum bipartite matching, CLRS#26.3\n- Prim's algorithm, CLRS#23.2: [java](/java-algorithm/src/main/java/com/example/algorithm/graph/Prim.java) | A minimum spanning tree algorithm. It is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph.\n\n```txt\nalgorithm Prim(G, root):\n    for each u in G.V:\n        u.key = ∞\n        u.parent = NIL\n    root.key = 0\n    Queue = G.V                                       // queue is a min priority queue\n    while Queue != ∅:\n        u = EXTRACT-MIN(Queue)\n        for each v in G.Adj[u]:\n            if v in Queue and w(u, v) \u003c v.key:\n                v.parent = u\n                v.key = w(u, v)\n```\n\n- Push-relabel algorithm, CLRS#26.4\n- Viterbi algorithm: Shortest stochastic path algorithm. It solves with additional probabilistic weights on each node.\n\n**Examples**\n\n\u003e NOTE: In the following, the C++ implementations are currently only available on MSVC.\n\n- Maze problem: [java](/java-algorithm/src/main/java/com/example/algorithm/graph/MazeProblem.java) | A maze problem is that find a path from the start to the goal. The maze is represented by a graph. The start and the goal are represented by vertices. The path is represented by a sequence of vertices.\n- Minimum spanning tree (Kruskal, Prim, Boruvka), CLRS#23, CCSP#4.4.2: [python(test)](/python-algorithm/algorithm/graph/test/test_minimum_spanning_tree.py) | Find the minimum spanning tree of a graph. cf. Kruskal(CLRS#23.2, CLRS#21.1), Prim(CLRS#23.2)\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :key: Hash table\n\n(CLRS#11)\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\n// map\nauto map = std::unordered_map\u003cstd::string, int\u003e{{\"a\", 1}, {\"b\", 2}};\ninsert({\"c\", 3}), emplace(\"d\", 4), find(\"b\"), end(), erase(\"a\"), size(), empty()\n\n// set\nauto set = std::unordered_set{1, 2, 3, 4, 5};\ninsert(42), emplace(42), find(42), end(), erase(42), size(), empty()\n\n// tuple\nauto t1 = std::tuple{-1, -1};\nauto t2 = std::make_tuple(-1, -1);\nauto [x, y] = t1;\n\n// transform\nstd::ranges::transform(nums, std::inserter(map, map.end()),\n    [i = 0](auto num) mutable { return std::pair{num, i++}; });\n```\n\n**Python declaration/functions**\n\n```python\n# set\nnumber_set: set[int] = set()\nadd(1), update([2, 3, 4])\n\n# dictionary\nsample_dict: dict[str, int] = {'a': 1, 'b': 2, 'c': 3}\n\n# defaultdict\nsample_dict: collections.defaultdict[str, int] = collections.defaultdict(int)\nsample_dict['a'] = 1\nsample_dict.update({'b': 2, 'c': 3})\n\n# counter\nsample_counter: collections.Counter = collections.Counter()\nsample_counter.update([1, 1, 2, 2, 3])\n```\n\n**Java declaration/methods**\n\n```java\nimport java.util.*;\n\n// map\nMap\u003cString, Integer\u003e map = new HashMap\u003c\u003e();\nput(\"a\", 1), putIfAbsent(\"b\", 2), get(\"a\"), getOrDefault(\"f\", 6), remove(\"a\"), size(), isEmpty(),\nkeySet(), values(), entrySet(), containsKey(\"a\"), containsValue(1), replace(\"a\", 2), clear()\nvar keys = map.keySet().toArray(String[]::new);\nvar values = map.values().toArray(Integer[]::new);\n\n// set\nSet\u003cInteger\u003e set = new HashSet\u003c\u003e();\nadd(1), remove(1), size(), isEmpty(), contains(1), clear(), iterator()\nvar arr = set.toArray(Integer[]::new);\n\n// enum map\nMap\u003cCity, Integer\u003e map = new EnumMap\u003c\u003e(City.class);\n\n// linked hash map, linked hash set\nMap\u003cString, Integer\u003e map = new LinkedHashMap\u003c\u003e();\nSet\u003cInteger\u003e set = new LinkedHashSet\u003c\u003e();\n\n// unboxing\nint[] result = map.entrySet().stream()\n        .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))\n        .map(Map.Entry::getKey)\n        .mapToInt(Integer::parseInt)\n        .toArray();\n\n// guava\nimport com.google.common.collect.*;\nMap\u003cString, Integer\u003e map = Maps.newHashMap();\nSet\u003cInteger\u003e set = Sets.newHashSet();\nMap\u003cCity, Country\u003e map = Maps.newEnumMap(City.class);\nMap\u003cString, Integer\u003e map = Maps.newLinkedHashMap();\nSet\u003cInteger\u003e set = Sets.newLinkedHashSet();\n\n// guava multiset (implements Multiset\u003cE\u003e)\nMultiset\u003cString\u003e multiset = HashMultiset.create();\nMultiset\u003cString\u003e multiset = TreeMultiset.create();\nMultiset\u003cString\u003e multiset = LinkedHashMultiset.create();\nMultiset\u003cString\u003e multiset = ConcurrentHashMultiset.create();\nMultiset\u003cString\u003e multiset = ImmutableMultiset.of(\"a\", \"b\", \"c\");\n\n// guava multimap (implements Multimap\u003cK, V\u003e)\nMultimap\u003cString, Integer\u003e multimap = ArrayListMultimap.create();\nMultimap\u003cString, Integer\u003e multimap = HashMultimap.create();\nMultimap\u003cString, Integer\u003e multimap = LinkedListMultimap.create();\nMultimap\u003cString, Integer\u003e multimap = LinkedHashMultimap.create();\nMultimap\u003cString, Integer\u003e multimap = TreeMultimap.create();\nMultimap\u003cString, Integer\u003e multimap = ImmutableListMultimap.of(\"a\", 1, \"a\", 2, \"b\", 3);\nMultimap\u003cString, Integer\u003e multimap = ImmutableSetMultimap.of(\"a\", 1, \"a\", 2, \"b\", 3);\n\n// guava bimap (implements BiMap\u003cK, V\u003e, Map\u003cK, V\u003e)\nBiMap\u003cString, Integer\u003e bimap = HashBiMap.create();\nBiMap\u003cString, Integer\u003e bimap = ImmutableBiMap.of(\"a\", 1, \"b\", 2);\nBiMap\u003cCity, Country\u003e bimap = EnumBiMap.create(City.class, Country.class);\nBiMap\u003cCity, Integer\u003e bimap = EnumHashBiMap.create(City.class);\n\n// guava table (implements Table\u003cR, C, V\u003e)\nTable\u003cVertex, Vertex, Double\u003e weightedGraph = HashBasedTable.create();\nTable\u003cVertex, Vertex, Double\u003e weightedGraph = TreeBasedTable.create();\nTable\u003cVertex, Vertex, Double\u003e weightedGraph = ArrayTable.create(Arrays.asList(v1, v2), Arrays.asList(v3, v4));\nTable\u003cVertex, Vertex, Double\u003e weightedGraph = ImmutableTable.of(v1, v2, 4.0);\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Examples**\n\n- Anonymous letter or words constructible, EPI#12.2: [cpp](/cpp-algorithm/src/hashtable/anonymous_letter.h)(`IsLetterConstructibleFromMagazine`, `IsWordConstructibleFromMagazine`) | Check if a letter can be written using the characters/words in a magazine.\n- Collatz conjecture, EPI#12.11: [cpp](/cpp-algorithm/src/hashtable/collatz_conjecture.h)(`FindNumbersSatisfyingCollatzConjecture`) | Find the numbers satisfying the Collatz conjecture.\n- Find anagrams: [cpp](/cpp-algorithm/src/hashtable/find_anagram.h)(`FindAnagramMappings`) | Given an array of strings, group anagrams together.\n- Find the smallest subarray\n  - covering all values, EPI#12.6: [cpp](/cpp-algorithm/src/hashtable/smallest_subarray.h)(`FindSmallestSubarrayCoveringSubset`) | Find the smallest subarray that covers all the elements in a set.\n  - sequentially covering all values, EPI#12.7: [cpp](/cpp-algorithm/src/hashtable/smallest_subarray.h)(`FindSmallestSubarraySequentiallyCoveringSubset`) | Find the smallest subarray that sequentially covers all the elements in a set.\n- ISBN cache, EPI#12.3: [cpp](/cpp-algorithm/src/hashtable/isbn_cache.h) | Implement an LRU (Least Recently Used) cache for ISBN lookups.\n- Nearest repeated entry, EPI#12.5: [cpp](/cpp-algorithm/src/hashtable/nearest_repeated_entry.h)(`FindNearestRepeatedEntry`) | Find the nearest repeated entry in an array of strings.\n- Optimized the lowest common ancestor (LCA), EPI#12.4: [cpp](/cpp-algorithm/src/hashtable/optimized_lca.h)(`FindOptimizedLowestCommonAncestor`) | Find the lowest common ancestor of two nodes in a binary tree using a hash table. This traverses together until node1 and node2 meet.\n- Palindromic permutation, EPI#12.1: [cpp](/cpp-algorithm/src/hashtable/check_palindromic_permutation.h)(`IsPalindromePermutation`) | Given a string, determine if a permutation of the string could form a palindrome.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :rocket: Heap\n\nA min-heap/max-heap is ideal for maintaining a collection of elements when we need to add arbitrary values and extract the smallest/largest element.\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\nauto queue = std::priority_queue\u003cint\u003e{};    // max heap\nauto queue = std::priority_queue\u003cint, std::vector\u003cint\u003e, std::greater\u003cint\u003e\u003e{};   // min heap\npush(1), emplace(2), pop(), top(), size(), empty()\n```\n\n**Python declaration/functions**\n\n```python\nnumber_list: list[int] = [5, 4, 3, 2, 1]\nheapq.heapify(number_list)\nheapq.nlargest(3, number_list), heapq.nsmallest(3, number_list)\nheapq.heappush(number_list, 6), heapq.heappop(number_list), heapq.heapreplace(number_list, 0)\n```\n\n**Java declaration/methods**\n\n```java\nimport java.util.*;\nQueue\u003cInteger\u003e queue = new PriorityQueue\u003c\u003e();\nQueue\u003cInteger\u003e queue = new PriorityQueue\u003c\u003e(Collections.reverseOrder());\nadd(1), peek(), poll(), remove(), size(), isEmpty(),\ncontains(1), clear(), iterator()\n\n// guava\nimport com.google.common.collect.*;\nQueue\u003cInteger\u003e queue = Queues.newPriorityQueue();\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Heap algorithms**\n\n- Fibonacci heap, CLRS#19\n\n**Examples**\n\n- Compute the k closest stars: [cpp](/cpp-algorithm/src/heap/closest_star.h)(`FindClosestStar`) | Find the $k$ closest stars to the earth. The stars are represented by a sequence of points(coordinates).\n- Compute the median of a sequence of numbers: [cpp](/cpp-algorithm/src/heap/online_data_median.h)(`FindMedian`) | Find the median of a sequence of numbers. The median is the number separating the higher half of a data sample from the lower half.\n- Merge sorted arrays: [cpp](/cpp-algorithm/src/heap/merge_sorted_array.h)(`MergeSortedArray`) | Merge k sorted arrays into one heap.\n- Sort an increasing-decreasing array: [cpp](/cpp-algorithm/src/heap/sort_increasing_decreasing_array.h)(`SortIncreasingDecreasingArray`) | Sort an array that is repeatedly increasing then decreasing.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :paperclips: Linked list\n\nA linked list is a data structure that consists of a sequence of elements, where each element points to the next. (CLRS#10.2)\u003cbr\u003eIn Python, there is no built-in type or library for LinkedList.\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\nauto list = std::list{1, 2, 3};   // doubly linked list\npush_front(4), emplace_front(5), push_back(6), emplace_back(7),\npop_front(), pop_back(), reverse(), sort(), insert(list.begin(), 11),\nemplace(list.end(), 12), splice(list.end(), std::list{8, 9, 10})\n\nauto list = std::forward_list{1, 2, 3};   // singly linked list\npush_front(4), emplace_front(5), pop_front(), reverse(), sort()\n```\n\n**Java declaration/methods**\n\n```java\nimport java.util.*;\n\n// doubly linked list\nList\u003cInteger\u003e list = new LinkedList\u003c\u003e();\nadd(1), addAll(List.of(2, 3, 4, 5)),\nremove(0), removeFirst(), removeLast(), removeIf(x -\u003e x % 2 == 0), subList(1, 3),\nget(0), getFirst(), getLast(), size(), isEmpty(), contains(1), containsAll(List.of(1, 2, 3)),\niterator(), listIterator()\n\n// dynamically resized array\nList\u003cInteger\u003e list = new ArrayList\u003c\u003e();\nadd(1), addAll(List.of(2, 3, 4, 5)), remove(0), subList(1, 3),\nget(0), size(), isEmpty(), contains(3), containsAll(List.of(3, 4)),\niterator(), listIterator()\n\n// guava\nimport com.google.common.collect.*;\nList\u003cInteger\u003e list = Lists.newLinkedList();\nList\u003cInteger\u003e list = Lists.newArrayList();\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Examples**\n\n- Add list-based integers, EPI#7.13: [cpp](/cpp-algorithm/src/linkedlist/list_arithmetic_operation.h)(`AddTwoNumbers`) | Add two numbers represented by linked list.\n- Delete nodes from linked list\n  - a node from linked list, EPI#7.6: [cpp](/cpp-algorithm/src/linkedlist/delete_list_node.h)(`DeleteNodeFromList`) | Delete a node from a linked list.\n  - the k-th last node from linked list, EPI#7.7: [cpp](/cpp-algorithm/src/linkedlist/delete_list_node.h)(`DeleteNodeKthLast`) | Delete the $k$-th last node from a linked list.\n  - duplicate nodes from sorted linked list, EPI#7.8: [cpp](/cpp-algorithm/src/linkedlist/delete_list_node.h)(`DeleteDuplicateNode`) | Delete duplicate nodes from a sorted linked list.\n- Implement cyclic right shift for a singly linked list, EPI#7.9: [cpp](/cpp-algorithm/src/linkedlist/shift_list.h)(`CyclicallyRightShiftList`) | Implement cyclic right shift for a singly linked list.\n- Linked list has a cycle, EPI#7.3: [cpp](/cpp-algorithm/src/linkedlist/list_cycle.h)(`HasCycle1`, `HasCycle2`, `HasCycle3`) | Determine that a linked list has a cycle.\n- List pivoting, EPI#7.12: [cpp](/cpp-algorithm/src/linkedlist/order_list_node.h)(`ListPivoting`) | Rearrange nodes smaller than pivot to the left and larger than pivot to the right.\n- Merge linked lists\n  - even and odd nodes in linked list, EPI#7.10: [cpp](/cpp-algorithm/src/linkedlist/merge_list.h)(`MergeEvenOddLinkedList`) | Merge even and odd nodes in a singly linked list.\n  - two sorted linked lists, EPI#7.1: [cpp](/cpp-algorithm/src/linkedlist/merge_list.h)(`MergeTwoSortedLinkedList`) | Merge two sorted linked lists. In worst-case, this task has $O(n + m)$ time complexity, where $n$ and $m$ are the length of the lists.\n- Palindrome list, EPI#7.11: [cpp](/cpp-algorithm/src/linkedlist/list_palindrome.h)(`IsListPalindrome`) | Determine that a linked list is a palindrome.\n- Reverse a single sublist, EPI#7.2: [cpp](/cpp-algorithm/src/linkedlist/reverse_list.h)(`ReverseSubList`) | Reverse a single sublist of a linked list.\n- Linked lists overlap\n  - two linked lists overlap, EPI#7.4: [cpp](/cpp-algorithm/src/linkedlist/list_cycle.h)(`OverlappingNoCycleList`) | Determine that two linked lists without cycle overlap.\n  - two linked lists with cycles overlap, EPI#7.5 [cpp](/cpp-algorithm/src/linkedlist/list_cycle.h)(`OverlappingCycleList`) | Determine that two linked lists with cycle overlap.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :walking: Queue\n\nA queue is a data structure that implements the FIFO (first-in, first-out) policy. It has the following operations: enqueue (insert an element), dequeue (delete the least recently inserted element). (CLRS#10.1)\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\nauto container = std::queue\u003cint\u003e{};\npush(1), emplace(2), pop(), front(), back(), size(), empty()\n\nauto container = std::deque\u003cint\u003e{};\npush_back(1), emplace_back(2), push_front(3), emplace_front(4),\npop_back(), pop_front(), front(), back(), size(), empty()\n```\n\n**Python declaration/functions**\n\n```python\ndeque: collections.deque = collections.deque([1, 2, 3, 4, 5])\ndeque[0], deque[-1]\nappend(6), appendleft(7), pop(), popleft()\n```\n\n**Java declaration/methods**\n\n```java\nimport java.util.*;\nDeque\u003cInteger\u003e deque = new ArrayDeque\u003c\u003e();\nadd(1), remove(), pop(), size(), isEmpty(), contains(1), clear(),\nofferFirst(6), offerLast(7), pollFirst(), pollLast(), peekFirst(), peekLast(),\naddFirst(8), addLast(9), removeFirst(), removeLast(), getFirst(), getLast(),\niterator(), descendingIterator()\n\nvar array = deque.toArray(Integer[]::new);  // deque to array\nvar list = new ArrayList\u003c\u003e(deque);          // deque to list\n\n// guava\nimport com.google.common.collect.*;\nDeque\u003cInteger\u003e deque = Queues.newArrayDeque();\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Examples**\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :hamburger: Stack\n\nA stack is a data structure that implements the LIFO (last-in, first-out) policy. It has the following operations: push (insert an element), pop (delete the most recently inserted element). (CLRS#10.1)\n\n**C++ declaration/methods**\n\n```cpp\nauto stack = std::stack\u003cint\u003e{};\npush(1), emplace(2), pop(), top(), size(), empty()\n```\n\n**Python declaration/functions**\n\n```python\n# use list type\nstack: list[int] = [1, 2, 3]\nstack[-1], len(stack)\nappend(4), pop()\n```\n\n**Java declaration/methods**\n\n```java\nimport java.util.*;\nStack\u003cInteger\u003e stack = new Stack\u003c\u003e();\npush(1), add(1, 2), addAll(anotherList), pop(), peek(), size(), isEmpty(),\ncontains(1), search(1), size(),\nremove(1), removeIf(x -\u003e x == 1), clear(),\niterator(), listIterator()\n\nvar array = stack.toArray(Integer[]::new);  // stack to array\nvar list = new ArrayList\u003c\u003e(stack);          // stack to list\n```\n\n**Examples**\n\n- Pair of bracket: [cpp](/cpp-algorithm/src/stack/check_pair_of_bracket.h)(`CheckPairOfBracket`) | Checks if the input string contains bracket pairs and is well-formed.\n- Print linked list in reverse order: [cpp](/cpp-algorithm/src/stack/print_stack.h)(`PrintLinkedListInReverseOrder`) | Print the linked list in reverse order using stack.\n- Max stack element: [cpp](/cpp-algorithm/src/stack/max_stack_element.h) | Implement stack that caches max value.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :deciduous_tree: Tree\n\nThe tree is a specific type of graph. A tree is an undirected graph in which any two vertices are connected by exactly one path. It is connected without cycles.\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods (binary search tree based)**\n\n```cpp\n// map\nauto map = std::map\u003cstd::string, int\u003e{{\"a\", 1}, {\"b\", 2}};\ninsert({\"c\", 3}), emplace(\"d\", 4), erase(\"a\"), find(\"b\"), size(), empty(), equal_range(\"c\")\n\n// set\nauto set = std::set{1, 2, 3, 4, 5};\ninsert(42), emplace(42), erase(42), find(42), size(), equal_range(3)\n```\n\n**Python declaration/functions (binary search tree based)**\n\n```python\n# sortedcontainers\nsort_list = SortedList([1, 2, 3, 4, 5])\nsort_set = SortedSet([1, 2, 3, 4, 5])\nsort_dict = SortedDict({'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5})\n```\n\n**Java declaration/methods (binary search tree based)**\n\n```java\nimport java.util.*;\n\n// tree map (based on red-black tree)\nMap\u003cInteger, Integer\u003e map = new TreeMap\u003c\u003e();\nMap\u003cInteger, Integer\u003e map = new TreeMap\u003c\u003e(Collections.reverseOrder());\nMap\u003cString, Integer\u003e map = new TreeMap\u003c\u003e(Map.of(\"a\", 1, \"b\", 2, \"c\", 3));\nput(\"a\", 1), putIfAbsent(\"b\", 2), get(\"a\"), getOrDefault(\"f\", 6), remove(\"a\"), size(), isEmpty(),\nkeySet(), values(), entrySet(), containsKey(\"a\"), containsValue(1), replace(\"a\", 2), clear()\nfirstKey(), lastKey(), lowerKey(\"b\"), higherKey(\"b\"), floorKey(\"b\"), ceilingKey(\"b\"),pollFirstEntry(), pollLastEntry(),\nheadMap(\"c\"), tailMap(\"c\"), subMap(\"a\", \"c\"), descendingMap(), descendingKeySet()\n\n// tree set (based on red-black tree)\nSet\u003cInteger\u003e set = new TreeSet\u003c\u003e();\nSet\u003cInteger\u003e set = new TreeSet\u003c\u003e(Collections.reverseOrder());\nSet\u003cInteger\u003e set = new TreeSet\u003c\u003e(List.of(1, 2, 3, 4, 5));\nadd(1), remove(1), size(), isEmpty(), contains(1), clear(), iterator(), descendingIterator(),\nfirst(), last(), lower(3), higher(3), floor(3), ceiling(3), pollFirst(), pollLast(),\nheadSet(3), tailSet(3), subSet(2, 4), descendingSet()\n\n// guava\nimport com.google.common.collect.*;\nMap\u003cInteger, Integer\u003e map = Maps.newTreeMap();\nSet\u003cInteger\u003e set = Sets.newTreeSet();\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Properties of Trees**\n\n- A tree with $n$ vertices has $n-1$ edges.\n- A full $m$-ary tree with $i$ internal vertices contains $n = mi + 1$ vertices. (cf. vertices $n$ = internal vertices $i$ + leaves $l$)\n- A full $m$-ary tree with\n  - $(i)$ $n$ vertices has $i = (n - 1)∕m$ internal vertices and $l = [(m - 1)n + 1]∕m$ leaves,\n  - $(ii)$ $i$ internal vertices has $n = mi + 1$ vertices and $l = (m - 1)i + 1$ leaves,\n  - $(iii)$ $l$ leaves has $n = (ml - 1)∕(m - 1)$ vertices and $i = (l - 1)∕(m - 1)$ internal vertices.\n- There are at most $m^h$ leaves in an $m$-ary tree of height $h$. $(l = m^h)$\n  - If an $m$-ary tree of height $h$ has $l$ leaves, then $h \\geq \\lceil \\log_{m}{l} \\rceil$.\n  - If the $m$-ary tree is full and balanced, then $h = \\lceil \\log_{m}{l} \\rceil$.\n\n**Balanced binary tree**\n\n- Depending on the balance: complete binary tree, full binary tree, perfect binary tree\n- Balanced binary tree: Red-black tree, AVL tree (from Adelson-Velsky and Landis)\n\n**Tree traversal in binary tree**\n\n- Preorder traversal (root, left, right): best choice for applications where internal vertices must be explored before leaves.\n- Inorder traversal (left, root, right): best choice for applications where internal vertices must be explored in-order.\n- Postorder traversal (left, right, root): best choice for applications where leaves need to be explored before internal vertices.\n\n**Tree algorithms**\n\n- AVL tree\n- B-tree, CLRS#18: [cpp](/cpp-algorithm/src/tree/b_tree.h) | B-tree is a self-balancing data structure which can have many child nodes. It is commonly used in auxiliary storage devices and database system. B-tree has the following properties: 1) Nodes have lower and upper bounds on the number of keys they can contain. (represent using degree $t$) 2) Every node other than the root must have at least $t-1$ keys. 3) Every node may contain at most $2t-1$ keys.\n- Binary search tree, CLRS#12: [cpp](/cpp-algorithm/src/tree/binary_search_tree.h) | In binary search tree, all internal nodes are stored in ordered state. If $y$ is a child of $x$ and $y$ is a node in the left subtree, then $y.key \\leq x.key$, and if $y$ is a node in the right subtree, then $y.key \\geq x.key$.\n- Red-black tree, CLRS#13\n- Trie\n- van Emde Boas tree (vEB tree), CLRS#20\n\n**Examples**\n\n- Balanced tree status: [cpp](/cpp-algorithm/src/tree/balanced_tree_status.h) | Whether the binary tree is balanced or not.\n- Binary tree exterior: [cpp](/cpp-algorithm/src/tree/binary_tree_exterior.h)(`CreateExteriorNodeList`) | Create a vector of exterior nodes in a binary tree.\n- Construct binary tree\n  - from preorder and inorder traversal: [cpp](/cpp-algorithm/src/tree/construct_binary_search_tree.h)(`ConstructTreeFromPreorderInorder`) | Construct a binary search tree from preorder and inorder traversal. This task has $O(n)$ time complexity.\n  - from preorder with marker: [cpp](/cpp-algorithm/src/tree/construct_binary_search_tree.h)(`ConstructTreeFromMarkerPreorder`) | Construct a binary search tree from preorder traversal with marker. This task has $O(n)$ time complexity.\n- Leaf node list: [cpp](/cpp-algorithm/src/tree/leaf_node_list.h)(`CreateLeafNodeList`) | Create a vector of leaf nodes in a binary tree.\n- Lowest common ancestor\n  - two nodes: [cpp](/cpp-algorithm/src/tree/lowest_common_ancestor.h)(`FindLowestCommonAncestor`) | Find the lowest common ancestor of two nodes in a binary tree.\n  - two nodes with parent pointer: [cpp](/cpp-algorithm/src/tree/lowest_common_ancestor.h)(`FindLowestCommonAncestor2`) | Find the lowest common ancestor of two nodes in a binary tree. The nodes have parent pointers.\n- Populate right sibling: [cpp](/cpp-algorithm/src/tree/right_sibling.h)(`PopulateRightSibling`) | Populate the right sibling of a binary tree.\n- Sum of root to leaf\n  - root to leaf path corresponding to the given sum: [cpp](/cpp-algorithm/src/tree/sum_root_to_leaf.h)(`HasKeySum`) | Whether the tree has a root-leaf path equal to the given sum.\n  - decimal and binary representation: [cpp](/cpp-algorithm/src/tree/sum_root_to_leaf.h)(`SumRootToLeafDecimal`, `SumRootToLeafBinary`) | Sum of all root to leaf paths in a binary tree (decimal and binary representation).\n- Tree symmetric: [cpp](/cpp-algorithm/src/tree/tree_symmetric.h) | Whether the binary tree is symmetric or not.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n## Topics\n\n### :jigsaw: Dynamic programming (DP)\n\n(CLRS#15)\n\n**Examples**\n\n- Fibonacci number, CCSP#1.1: [cpp](/cpp-algorithm/src/dp/fibonacci_number.h),[python](/python-algorithm/algorithm/dp/fibonacci.py) | Fibonacci sequence is a sequence of numbers where each number is the sum of the two preceding numbers. Fibonacci number is $n$-th number in the sequence. The Fibonacci sequence is defined as follows:\n  - $F_0 = 0$\n  - $F_1 = 1$\n  - $F_n = F_{n-1} + F_{n-2}$ (for $n \u003e 1$)\n- Interval subset sum: [cpp](/cpp-algorithm/src/dp/interval_subset_sum_problem.h)(`SubsetSum1`, `SubsetSum2`, `DivideAndConquerSubsetSum`, `DynamicProgrammingSubsetSum`) | Interval subset sum problem is that finds the maximum sum of a subset of intervals.\n- Longest common subsequence, CLRS#15.4: [cpp](/cpp-algorithm/src/dp/longest_common_subsequence.h)\n- Rod cutting, CLRS#15.1: [cpp](/cpp-algorithm/src/dp/rod_cutting.h) | Rod cutting is a problem of cutting a rod into pieces of a given length to determine the maximum profit.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :clock9: Greedy\n\n(CLRS#16)\n\n**Examples**\n\n- Activity selection problem, CLRS#16.1: [cpp](/cpp-algorithm/src/greedy/activity_selection.h) | Activity selection problem using greedy algorithm or recursive approach. This is similar to the Interval scheduling problem.\n- Cashier's change: [python](/python-algorithm/algorithm/greedy/cashier_change.py) | Cashier's change problem is that finds the minimum number of coins required to make change for a given amount of money.\n- Huffman code, CLRS#16.3: [cpp](/cpp-algorithm/src/greedy/huffman_code.h) | Huffman code constructs optimal prefix codes. This is always represented by a full binary tree.\n- Interval scheduling: [python](/python-algorithm/algorithm/greedy/interval_schedule.py) | Interval scheduling problem is that finds the minimum number of intervals required to schedule a set of activities(lectures).\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :triangular_ruler: Mathematics\n\n**C++ declaration/methods**\n\n```cpp\nstd::numeric_limits\u003cint\u003e::min(), std::numeric_limits\u003cfloat\u003e::max(), std::numeric_limits\u003cdouble\u003e::infinity()\n// cmath\nstd::abs(-34), std::fabs(-3.14), std::ceil(2.17), std::floor(3.14), std::min(x, -4), std::max(3.14, y),\npow(2.17, 3.14), log(7.12), sqrt(225)\n```\n\n**Python declaration/functions**\n\n```python\nfloat('inf'), float('-inf')\n# math\nmath.inf, -math.inf\nmath.fabs(-34.5), math.ceil(2.17), math.floor(3.14), math.max(x, -3), math.min(x, 3.14),\nmath.pow(2.71, 3.15), math.round(3.14), math.sqrt(225)\n```\n\n**Java declaration/methods**\n\n```java\nInteger.MIN_VALUE, Float.MAX_VALUE, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Boolean.TRUE\n// math\nMath.abs(-34.5), Math.ceil(2.17), Math.floor(3.14), Math.max(x, -3), Math.min(x, 3.14),\nMath.pow(2.71, 3.15), Math.round(3.14), Math.sqrt(225)\n```\n\n**Mathematical algorithms**\n\n- Combination: [cpp](/cpp-algorithm/src/math/combination.h)(`GenerateCombination`) | Find the number of ways to choose $k$ items from $n$ items.\n- Fast Fourier transform: Fast Fourier transform is a mathematical algorithm that finds the discrete Fourier transform of a set of real numbers.\n- Greatest common divisor (GCD), CLRS#31.2: [python](/python-algorithm/algorithm/math/greatest_common_divisor.py), [golang](/go-algorithm/pkg/math/greatest_common_divisor.go), [java](/java-algorithm/src/main/java/com/example/algorithm/math/GreatestCommonDivisor.java) | Find the greatest common divisor of two numbers.\n- Integer factorization: [java](/java-algorithm/src/main/java/com/example/algorithm/math/IntegerFactorization.java) | Integer factorization is the process of determining which prime numbers divide a given positive integer.\n- Least common multiple (LCM): [python](/python-algorithm/algorithm/math/least_common_multiple.py), [golang](/go-algorithm/pkg/math/least_common_multiple.go), [java](/java-algorithm/src/main/java/com/example/algorithm/math/LeastCommonMultiple.java) | Find the least common multiple of two numbers.\n- Miller-Rabin primality test, CLRS#31.8: [cpp](/cpp-algorithm/src/math/miller_rabin.h) | Miller-Rabin primality test is a mathematical algorithm that finds whether a given number is prime.\n- Permutation\n  - given string: [cpp](/cpp-algorithm/src/math/permutation.h)(`Permutation`) | Find the permutation of a set of items from given string.\n  - elements of an array, EPI#5.10: [cpp](/cpp-algorithm/src/math/permutation.h)(`ApplyPermutationWithAdditionalSpace`, `ApplyPermutationBySwap`) | Permute the elements of an array\n  - inverse permutation: [cpp](/cpp-algorithm/src/math/permutation.h)(`InversePermutation`)\n  - next/previous permutation, EPI#5.11: [cpp](/cpp-algorithm/src/math/permutation.h)(`NextPermutation`, `PreviousPermutation`) | Compute the next/previous permutation.\n  - $k$-th permutation, EPI#5.11: [cpp](/cpp-algorithm/src/math/permutation.h)(`KthPermutation`) | Compute the $k$-th permutation.\n- Prime number: [java](/java-algorithm/src/main/java/com/example/algorithm/math/PrimeNumber.java)(`isPrime`) | Check whether a given number is prime.\n- Simplex algorithm: Simplex algorithm is a mathematical algorithm that finds the optimal solution to a linear programming problem.\n- System of linear equations: System of linear equations is a mathematical algorithm that finds the solution to a system of linear equations.\n\n**Examples**\n\n- Base expansion (base $b$ expansion of $n$): [python](/python-algorithm/algorithm/math/base_expansion.py) | Constructing the base $b$ expansion of an integer $n$. Such as binary, octal, decimal, hexadecimal expansion, etc.\n- Binary addition: [python](/python-algorithm/algorithm/math/binary_operation.py) | Add two binary numbers which are represented as strings.\n- Inverse of matrix: Inverse of matrix is a mathematical algorithm that finds the inverse of a matrix.\n- Matrix multiplication: [python](/python-algorithm/algorithm/math/matrix_multiplication.py) | This is the product of two matrices.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :1234: Primitive type\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\nstd::to_string(42), std::swap(x, y)\nstd::numeric_limits\u003cint\u003e::min(), std::numeric_limits\u003cfloat\u003e::max(), std::numeric_limits\u003cdouble\u003e::infinity()\nstd::abs(-34), std::fabs(-3.14), std::ceil(2.17), std::floor(3.14), std::min(x, -4), std::max(3.14, y), pow(2.17, 3.14), log(7.12), sqrt(225) // cmath\nstd::stoi(\"42\"), std::stod(\"3.14\"), std::stoi(\"42\", nullptr, 16), std::stoi(\"1000010\", nullptr, 2) // string -\u003e int/double/hex/binary\nstd::bitset\u003c8\u003e(42), std::bitset\u003c8\u003e(3.14), std::bitset\u003c8\u003e(0x42), std::bitset\u003c8\u003e(0b1000010) // int/double/hex/binary -\u003e bitset\nstd::uniform_int_distribution\u003cint\u003e distribution(1, 6), std::uniform_real_distribution\u003cdouble\u003e distribution(0.0, 1.0) // random\n\n// random values\nstd::random_device rd;\nstd::mt19937 generator(rd());\nstd::uniform_int_distribution distribution(1, 10);  // integer in [1, 10]\nconst auto i = distribution(generator);\nconst auto d = std::generate_canonical\u003cdouble, 10\u003e(generator);  // floating point number in [0, 1)\n```\n\n**Python declaration/functions**\n\n```python\nfloat('inf'), float('-inf'),\nmath.inf, -math.inf,\nmath.fabs(-34.5), math.ceil(2.17), math.floor(3.14), math.max(x, -3), math.min(x, 3.14),\nmath.pow(2.71,3.15), math.round(3.14), math.sqrt(225)\nabs(-34), min(number_list), max(number_list), sum(number_list), sorted(number_list)\nlen(sample_string), len(number_list), len(sample_dict)  # length\nstr(42), str(3.14), str(True)           # int/float/bool -\u003e string\nint(\"42\"), float(\"3.14\"), bool(\"true\")  # string -\u003e int/float/bool\nint(\"1000010\", 2), int(\"52\", 8), int(\"2a\", 16)  # string -\u003e binary/octal/hex\nbin(42), oct(42), hex(42)       # int -\u003e binary/octal/hex\nascii('a'), chr(97), ord('a')   # unicode \u003c-\u003e ascii code\n\n# copy\ncopy.deepcopy(number_list) # deep copy\ncopy.copy(number_list)     # shallow copy\n\n# random\nrandom.randrange(28)       # [0, 28)\nrandom.randrange(1, 100)   # [1, 100)\nrandom.randrange(8, 16)    # [8, 16)\nrandom.randrange(8, 16, 2) # [8, 16) with step 2\nrandom.shuffle(number_list)\nrandom.choice(number_list)\n```\n\n**Java declaration/methods**\n\n```java\nInteger.MIN_VALUE, Float.MAX_VALUE, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Boolean.TRUE\nMath.abs(-34.5), Math.ceil(2.17), Math.floor(3.14), Math.max(x, -3), Math.min(x, 3.14), Math.pow(2.71, 3.15), Math.round(3.14), Math.sqrt(225) // math\nInteger.valueOf(\"1\"), Double.valueOf(\"3.14\"), Boolean.valueOf(\"true\"), Float.toString(3.14f)  // reference type\nInteger.parseInt(\"42\"), Double.parseDouble(\"3.14\")  // primitive type\nDouble.compare(x, 1.23) == 0, Integer.compare(x, 2) == 0  // comparing values\n\n// bitwise operation\nInteger.parseInt(\"1000010\", 2), Integer.parseInt(\"52\", 8), Integer.parseInt(\"2a\", 16) // string -\u003e binary/octal/hex\nInteger.toBinaryString(42), Integer.toHexString(42), Integer.toOctalString(42)      // int -\u003e binary/hex/octal string\nInteger.toString(num, base)     // int -\u003e string with base\nInteger.bitCount(42)            // number of 1-bits\nLong.parseLong(\"1000010\", 2), Long.parseLong(\"52\", 8), Long.parseLong(\"2a\", 16) // string -\u003e binary/octal/hex\nLong.toBinaryString(42), Long.toHexString(42), Long.toOctalString(42)       // long -\u003e binary/hex/octal string\nLong.toString(num, base)    // long -\u003e string with base\nLong.bitCount(42)           // number of 1-bits\n\n// bitset\nimport java.util.*;\nnew BitSet(16), set(0), set(0, 8), set(0, 8, true)\n\n// hex digits\nimport java.util.*;\nHexFormat hex = HexFormat.of();\nbyte b = 127;\nString byteStr = hex.toHexDigits(b);\n\n// random values\nimport java.util.Random;\nvar random = new Random();\nvar randomInt = random.nextInt(100);      // [0, 100)\nvar randomLong = random.nextLong();       // [0, 2^48)\nvar randomDouble = random.nextDouble();   // [0.0, 1.0)\nvar randomBoolean = random.nextBoolean(); // true/false\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Primitive type algorithms**\n\n- Arithmetic operation, EPI#4.5, EPI#4.6: [cpp](/cpp-algorithm/src/primitive/arithmetic_operation.h)(`Multiply`, `Divide`) | Calculate the product/fraction of two numbers without using arithmetic operators.\n- Power operation, EPI#4.7: [cpp](/cpp-algorithm/src/primitive/power_operation.h) | Compute repeated squaring $x^y$.\n\n**Examples**\n\n- Computing parity of word, EPI#4.1: [cpp](/cpp-algorithm/src/primitive/compute_parity.h)(`CountBits`, `Parity`, `ParityDropLowestBits`, `ParityLookupTable`)\n- Generate random number, EPI#4.10: [cpp](/cpp-algorithm/src/primitive/generate_random_number.h) | Generate a random number in a range with equal probability.\n- Integer palindrome, EPI#4.9: [cpp](/cpp-algorithm/src/primitive/integer_palindrome.h) | Check if a number is a palindrome.\n- Rectangle intersection, EPI#4.11: [cpp](/cpp-algorithm/src/primitive/rectangle_intersection.h) | Check if two rectangles intersect.\n- Reverse digits, EPI#4.8: [cpp](/cpp-algorithm/src/primitive/reverse_digit.h) | Reverse the digits of a given integer.\n- Swap bit, EPI#4.2: [cpp](/cpp-algorithm/src/primitive/swap_bit.h) | Swap the bits at indices $i$ and $j$.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :mag: Search\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\n// map\nauto map = std::map\u003cstd::string, int\u003e{{\"a\", 1}, {\"b\", 2}};\ninsert({\"c\", 3}), emplace(\"d\", 4), erase(\"a\"), find(\"b\"), size(), empty(), equal_range(\"c\")\n\n// set\nauto set = std::set{1, 2, 3, 4, 5};\ninsert(42), emplace(42), erase(42), find(42), size(), equal_range(3)\n\n// algorithm\nstd::ranges::find(v, 42)\nstd::ranges::find(v, 42)\nstd::ranges::find_if(v, [](auto x) { return x % 2 == 0; })\nstd::ranges::find_end(v, sub_v).begin() // result - v.begin()\nstd::ranges::binary_search(v, 42)\nstd::ranges::lower_bound(v, 42)\nstd::ranges::upper_bound(v, 42)\n```\n\n**Python declaration/functions**\n\n```python\nbisect.bisect_left(number_list, 3), bisect.bisect_right(number_list, 3), bisect.bisect(number_list, 3)\n```\n\n**Java declaration/methods**\n\n```java\nimport java.util.*;\n\nint[] array = new int[]{1, 2, 3, 4, 5};\nList\u003cInteger\u003e arrayList = new ArrayList\u003c\u003e(List.of(1, 2, 3, 4, 5));\nList\u003cInteger\u003e linkedList = new LinkedList\u003c\u003e(List.of(1, 2, 3, 4, 5));\nSet\u003cInteger\u003e hashSet = new HashSet\u003c\u003e(List.of(1, 2, 3, 4, 5));\nSet\u003cInteger\u003e linkedHashSet = new LinkedHashSet\u003c\u003e(List.of(1, 2, 3, 4, 5));\nSet\u003cInteger\u003e treeSet = new TreeSet\u003c\u003e(List.of(1, 2, 3, 4, 5));\n\n// binary search\nArrays.binarySearch(array, 3)             // for array\nCollections.binarySearch(arrayList, 3);   // for list\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Search algorithms**\n\n- Binary search: [python](/python-algorithm/algorithm/search/binary_search.py) | Binary search is a search algorithm that finds the position of a target value within a sorted array.\n- Integer square root, EPI#11.4: [cpp](/cpp-algorithm/src/search/compute_square_root.h)(`ComputeIntegerSquareRoot`) | Compute the integer square root of a given integer. This function returns the largest integer whose square is less than or equal to the given integer.\n- Linear search: [python](/python-algorithm/algorithm/search/linear_search.py) | Linear search is a search algorithm that compares x successively with each term of the list until a match is found.\n- Quick select algorithm: [cpp](/cpp-algorithm/src/search/quick_select.h)(`QuickSelectAlgorithm`) | QuickSelect is an algorithm used to select the k-th smallest (or largest) element in an unordered list of elements.\n\n**Examples**\n\n- DNA search (Search a codon(combinations of three nucleotides) in a gene), CCSP#2.1: [python](/python-algorithm/algorithm/search/dna_search.py)(`linear_contains`, `binary_contains`) | Search a codon(combinations of three nucleotides) in a gene using linear search and binary search.\n- Find k-th smallest/largest element in an array, EPI#11.8: [cpp](/cpp-algorithm/src/search/quick_select.h)(`FindKthSmallestElement`, `FindKthLargestElement`) | Find the k-th smallest/largest element in an array using the quickselect algorithm (`QuickSelectAlgorithm`).\n- Find the minimum and maximum elements in an array, EPI#11.7: [cpp](/cpp-algorithm/src/search/find_min_max.h)(`FindMinMax`)\n- Search an element in generic list, CCSP#2.1: [python](/python-algorithm/algorithm/search/generic_search.py)(`generic_linear_contains`, `generic_binary_contains`) | Search an element in generic list using linear search or binary search.\n- Search a sorted array\n  - entry equal to its index, EPI#11.2: [cpp](/cpp-algorithm/src/search/search_sorted_array.h)(`SearchEntryEqualToItsIndex`)\n  - the first greater than a key: [cpp](/cpp-algorithm/src/search/search_sorted_array.h)(`SearchFirstGreaterThanKey`)\n  - the first occurrence of a key, EPI#11.1: [cpp](/cpp-algorithm/src/search/search_sorted_array.h)(`SearchFirstOfKey`)\n  - search a cyclically sorted array for the smallest element, EPI#11.3: [cpp](/cpp-algorithm/src/search/search_sorted_array.h)(`SearchSmallestElementInCyclicallySortedArray`)\n- Search in a 2D sorted array(matrix), EPI#11.6: [cpp](/cpp-algorithm/src/search/search_sorted_matrix.h)(`SearchSortedMatrix`) | Search in a 2D sorted array(matrix) for a given element.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :abc: Sort\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\nstd::ranges::sort(v);         // introsort (quick sort + heap sort + insertion sort)\nstd::ranges::stable_sort(v);  // merge sort\n```\n\n**Python declaration/functions**\n\n```python\nnumber_list: list[int] = [1, 2, 3, 4, 5]\nnumber_list.sort()              # in-place\nresult = sorted(number_list)    # return a new list(copy)\n```\n\n**Java declaration/methods**\n\n`Arrays.sort()` and `Collections.sort()` sort the array and list in ascending order in-place.\n\n```java\nimport java.util.*;\n\n// Arrays\nArrays.sort(arr);           // dual pivot quick sort (primitive types)\n                            // timsort (insertion sort + merge sort) (reference types)\nArrays.sort(arr, Comparator.comparingInt(String::length));\nArrays.sort(arr, Comparator.comparingInt(String::length).reversed());\n\n// Collections\nCollections.sort(list);     // timsort (insertion sort + merge sort)\nlist.sort(Comparator.naturalOrder());\nlist.sort(Comparator.reverseOrder());\nlist.sort(Comparator.comparingInt(String::length));\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**Sorting algorithms**\n\n- Bubble sort, CLRS#2-problems2.2: [golang](go-algorithm/pkg/sort/bubble_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/BubbleSort.java) | Bubble sort is a sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items, and swaps them if needed.\u003cbr\u003e($\\textit{n}$ is the number of elements)\n\n| **Case**    | **Time complexity** | **Remarks**                                                                             |\n| :---------- | :-----------------: | :-------------------------------------------------------------------------------------- |\n| **Best**    |       $O(n)$        | when the input list is already sorted in the desired order (ascending or descending)    |\n| **Worst**   |      $O(n^2)$       | when the input list is already sorted in the reverse order of the desired sorting order |\n| **Average** |      $O(n^2)$       | when the input list is in jumbled order                                                 |\n\n- Bucket sort, CLRS#8.4: [golang](go-algorithm/pkg/sort/bucket_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/BucketSort.java) | Bucket sort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket contains a range of values and the elements are sorted within these buckets using any of the suitable sorting algorithms (such as insertion sort, merge sort, selection sort).\u003cbr\u003e($\\textit{n}$ is the number of elements and $\\textit{k}$ is the number of buckets)\n\n| **Case**    | **Time complexity** | **Remarks**                                                                                                    |\n| ----------- | :-----------------: | -------------------------------------------------------------------------------------------------------------- |\n| **Best**    |     $O(n + k)$      | when input elements are uniformly distributed\u003cbr\u003e and each bucket contains roughly the same number of elements |\n| **Worst**   |      $O(n^2)$       | when all elements are placed into a single bucket                                                              |\n| **Average** |       $O(n)$        |                                                                                                                |\n\n- Counting sort, CLRS#8.2: [golang](go-algorithm/pkg/sort/counting_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/CountingSort.java) | Counting sort is a non-comparative sorting algorithm that sorts the elements of an array by counting the occurrences of each element in the array. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array. It is used as a subroutine in radix sort (CLRS#8.3).\u003cbr\u003e($\\textit{n}$ is the number of elements and $\\textit{k}$ is the range of input values)\n\n| **Case**    | **Time complexity** | **Remarks**                                             |\n| ----------- | :-----------------: | ------------------------------------------------------- |\n| **Best**    |     $O(n + k)$      | when the input elements have a small range of values    |\n| **Worst**   |     $O(n + k)$      | when the input elements have a large range of values    |\n| **Average** |     $O(n + k)$      | when the elements are distributed randomly in the array |\n\n- Heap sort, CLRS#6: [golang](go-algorithm/pkg/sort/heap_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/HeapSort.java) | Heap sort is a comparison-based sorting algorithm that uses a binary heap data structure to sort an array. It is used for the implementation of priority queue.\u003cbr\u003e($\\textit{n}$ is the number of elements)\n\n| **Case**    | **Time complexity** | **Remarks** |\n| ----------- | :-----------------: | ----------- |\n| **Best**    |    $O(n log n)$     |             |\n| **Worst**   |    $O(n log n)$     |             |\n| **Average** |    $O(n log n)$     |             |\n\n- Insertion sort, CLRS#2.1: [cpp](/cpp-algorithm/src/sort/sort.h), [golang](go-algorithm/pkg/sort/insertion_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/InsertionSort.java) | Insertion sort is a comparison-based sorting algorithm that builds the final sorted array one element at a time. One of the fastest algorithms for sorting very small arrays (around 10 elements).\u003cbr\u003e($\\textit{n}$ is the number of elements)\n\n| **Case**    | **Time complexity** | **Remarks**                                                                     |\n| ----------- | :-----------------: | ------------------------------------------------------------------------------- |\n| **Best**    |       $O(n)$        | if the list is already sorted.\u003cbr\u003ethis case has linear running time             |\n| **Worst**   |      $O(n^2)$       | if the list is sorted in reverse order.\u003cbr\u003ethis case has quadratic running time |\n| **Average** |      $O(n^2)$       | this case has quadratic running time                                            |\n\n- Merge sort, CLRS#2.3: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/MergeSort.java) | divide and conquer algorithm\n\n| **Case**    | **Time complexity** | **Remarks**                                                                                           |\n| ----------- | :-----------------: | ----------------------------------------------------------------------------------------------------- |\n| **Best**    |    $O(n log n)$     | running time of sorting for input length $n$ is $T(n)$.\u003cbr\u003e$T(n) = 2T(n/2) + O(n) \\approx O(n log n)$ |\n| **Worst**   |    $O(n log n)$     |                                                                                                       |\n| **Average** |    $O(n log n)$     |                                                                                                       |\n\n- Quick sort, CLRS#7: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/QuickSort.java) | divide and conquer algorithm\n\n| **Case**    | **Time complexity** | **Remarks** |\n| ----------- | :-----------------: | ----------- |\n| **Best**    |    $O(n log n)$     |             |\n| **Worst**   |      $O(n^2)$       |             |\n| **Average** |    $O(n log n)$     |             |\n\n- Selection sort: [cpp](/cpp-algorithm/src/sort/sort.h), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/SelectionSort.java)\n\n| **Case**    | **Time complexity** | **Remarks**                                                                          |\n| ----------- | :-----------------: | ------------------------------------------------------------------------------------ |\n| **Best**    |      $O(n^2)$       | if the list is already sorted                                                        |\n| **Worst**   |      $O(n^2)$       | when sorted in ascending order, if you want to sort in descending order (vice versa) |\n| **Average** |      $O(n^2)$       | when the input list is in jumbled order                                              |\n\n**Examples**\n\n- H-Index, EPI#13.3: [cpp](/cpp-algorithm/src/sort/h_index.h) | Compute the researcher's h-index given a citation count array. The h-index is the largest number $\\textit{h}$ such that $\\textit{h}$ articles have at least $\\textit{h}$ citations each.\n- Intersection of two sorted arrays, EPI#13.1: [cpp](/cpp-algorithm/src/sort/intersect_two_sorted_array.h)(`IntersectTwoSortedArray`) | Compute the intersection of two sorted array. The input arrays may have duplicate entries, but the output should be free of duplicates.\n- Merge intervals, EPI#13.7, LeetCode#merge-intervals: [cpp](/cpp-algorithm/src/sort/merge_interval.h)(`MergeIntervals`) | Given a collection of intervals, merge all overlapping intervals.\n- Merge two sorted arrays, EPI#13.2, LeetCode#merge-sorted-array: [cpp](/cpp-algorithm/src/sort/merge_two_sorted_array.h)(`MergeTwoSortedArray`) | Merge two sorted array. Merge the second array into the first array.\n- Partitioning and sorting an array with many repeated entries, EPI#13.9: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/SortRepeatedEntry.java)(`groupByAge`) | Given an array of objects with an age field, reorder the array so that objects of equal age appear together. They should be sorted in ascending order of age, and the order of objects with the same age is not important.\n- Remove first-name duplicates, EPI#13.4: [cpp](/cpp-algorithm/src/sort/remove_first_name_duplicate.h)(`EliminateFirstNameDuplicate`) | Given an array of names, remove the duplicates of the first name.\n- Salary threshold, EPI#13.12: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/SalaryThreshold.java)(`computeSalaryThreshold`) | Given an array of salaries and a budget, compute the salary cap so that the total salaries equal the budget.\n- Team photo day, EPI#13.10: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/SortPlayerByHeight.java) | Given two arrays of numbers, for team photos, players are arranged in front and back rows and then photographed. The players in the back row must necessarily be taller than those in the front row. Additionally, all players in a row should belong to the same team.\n- Union of intervals, EPI#13.8: [cpp](/cpp-algorithm/src/sort/union_interval.h)(`UnionOfIntervals`) | Given a set of intervals, compute their union.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n### :page_facing_up: String\n\n\u003c!-- @formatter:off --\u003e\n\n**C++ declaration/methods**\n\n```cpp\nauto str = std::string{\"hello\"};\nappend(\"_world\"), push_back('!'), pop_back(), insert(5, \"_world\"), substr(0, 5), compare(\"hello_world\")\n\n// string stream\nstd::stringstream ss(str);\ngood(), bad(), fail(), eof(), clear(), operator\u003c\u003c(), operator\u003e\u003e()\n```\n\n**Python declaration/functions**\n\n```python\nhello_world: str = 'hello world'\nlen(hello_world), count('l'), find('world'), rfind('world'), index('world'), rindex('world'),\nstrip(), split(' '), replace(' ', ''), startswith('hello'), endswith('world'),\nlower(), upper(), capitalize(), title(), swapcase()\n\n# string concatenation\ns = s[6:]\ns += 'abc'\n```\n\n**Java declaration/methods**\n\n```java\nvar str = \"Hello World\";\nlength(), charAt(0), substring(0, 5), indexOf(\"Java\"), lastIndexOf(\"Java\"),\ncontains(\"Java\"), startsWith(\"Hello\"), endsWith(\"World\"),\ncompareTo(\"Hello Java\"), compareToIgnoreCase(\"hello world\"),\nconcat(\"!\"), replace(\"World\", \"Java\"), toUpperCase(), toLowerCase(), trim(),\ntoCharArray(), chars()\n\n// static methods\nString.format(\"%s %s\", \"Hello\", \"World\")\nString.join(\" \", \"Hello\", \"World\")\nString.valueOf(123)\n\n// string builder\nvar sb = new StringBuilder();\nappend(\"!\"), insert(0, \"Hello\"), delete(0, 5), deleteCharAt(0),\nlength(), charAt(0), indexOf(\"Java\"), lastIndexOf(\"Java\"),\nreverse(), replace(0, 5, \"World\"), substring(0, 5), toString(),\nsubSequence(0, 5), chars()\n\n// character\nvar ch = new Character('a');\nCharacter.isDigit('0'), Character.isLetter('a'), Character.isLetterOrDigit('a'), Character.isAlphabetic('a'),\nCharacter.isLowerCase('a'), Character.isUpperCase('A'), Character.toLowerCase('A'), Character.toUpperCase('a'),\nCharacter.isWhitespace(' ')\n\n// list/stack/deque to string\nvar str = collection.stream()\n    .map(String::valueOf)\n    .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append);\n```\n\n\u003c!-- @formatter:on --\u003e\n\n**String algorithms**\n\n- Finite automata, CLRS#32.3\n- Knuth-Morris-Pratt algorithm (KMP), CLRS#32.4\n- Naive string matching, CLRS#32.1: [cpp](/cpp-algorithm/src/string/naive_string_match.h), [python](/python-algorithm/algorithm/string/naive_string_search.py) | Find all occurrences of a pattern in a string.\n- Rabin-Karp algorithm, CLRS#32.2, EPI#6.12: [cpp](/cpp-algorithm/src/string/rabin_karp.h) | Use the hash function to find all occurrences of a pattern in a string. It has $\\theta(\\text{pattern-size})$ preprocessing time and $\\theta((\\text{text-size} - \\text{pattern-size} + 1) \\text{pattern-size})$ time complexity.\n\n**Examples**\n\n- Bit compression, CCSP#1.2: [python](/python-algorithm/algorithm/string/bit_compression.py) | Compress a gene string using bit manipulation.\n- Convert string, EPI#6.1: [cpp](/cpp-algorithm/src/string/convert_string.h)(`IntToString`, `StringToInt`) | Convert integer to string and vice versa.\n- IP address validation, EPI#6.9: [cpp](/cpp-algorithm/src/string/ip_address.h) | Validate IPv4 address that is in the form of 'x.x.x.x' where $\\textit{x}$ is a number between 0 and 255.\n- Look and say problem, EPI#6.7: [cpp](/cpp-algorithm/src/string/look_and_say.h)\n- Palindrome, EPI#6.5: [cpp](/cpp-algorithm/src/string/palindrome.h) | Check if a string is palindromic.\n- Print sine wave pattern string, EPI#6.10: [cpp](/cpp-algorithm/src/string/print_string.h)(`SineWaveString` and `PrintSineWaveString`) | Print a string in sine wave pattern.\n- Roman number string\n  - verify roman number string, EPI#6.8: [cpp](/cpp-algorithm/src/string/roman_number.h)(`VerifyRomanString`) | Verify if a string is a valid roman number.\n  - convert roman number string to integer, EPI#6.8: [cpp](/cpp-algorithm/src/string/roman_number.h)(`RomanStringToInteger`) | Convert a roman number to integer.\n- Run-length encoding (RLE), EPI#6.11: [cpp](/cpp-algorithm/src/string/run_length_encoding.h) | Run-length encoding is a simple form of data compression in which runs of data are stored as a single data value and count.\n- Spreadsheet column decoding/encoding, EPI#6.3: [cpp](/cpp-algorithm/src/string/sheet_encoding.h)(`DecodingSheetColumnId`, `EncodingSheetColumnId`) | Convert a spreadsheet column id to integer and vice versa.\n\n[:arrow_up_small: back to toc](#table-of-contents)\n\n## References\n\n- Introduction to Algorithms, 3rd Edition, by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein (CLRS)\n- Discrete Mathematics and Its Applications, 8th Edition, by Kenneth H. Rosen\n- Cracking the Coding Interview, 6th Edition, by Gayle Laakmann McDowell | _CTCI_\n- Elements of Programming Interviews, 2nd Edition, by Adnan Aziz, Tsung-Hsien Lee and Amit Prakash | _EPI_\n- Classic Computer Science Problems in Python, by David Kopec | _CCSP_\n\n[:arrow_up_small: back to toc](#table-of-contents)\n","funding_links":["https://github.com/sponsors/codejsha"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodejsha%2Falgorithm-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodejsha%2Falgorithm-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodejsha%2Falgorithm-examples/lists"}