What Is a Greedy Algorithm in C++ and How Is It Implemented?
A greedy algorithm takes the best local choice at each step. See when it works and how to implement it in modern C++ with tested examples.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
A greedy algorithm takes the best local choice at each step. See when it works and how to implement it in modern C++ with tested examples.
Dijkstra finds shortest paths from a source, Prim builds a minimum spanning tree. See both in modern C++ with priority queues and complexity.
Prim's algorithm builds a minimum spanning tree edge by edge. See a tested C++20 implementation using a priority queue with O(E log V) time.
Dijkstra's algorithm finds shortest paths from a source vertex when weights are non-negative. This guide shows a modern C++17 implementation using a min-heap.
DFS dives down a branch with a stack, BFS sweeps level by level with a queue. Compare both in C++ with full code, complexity and use cases.
BFS visits a graph level by level using a queue. This guide implements it in modern C++ with distances, path recovery and a grid example.
Implement depth-first search in C++ with recursion and an explicit stack, handle disconnected graphs and grids, and understand its O(V + E) cost.
Radix sort handles integers digit by digit, bucket sort scatters values by range. See C++20 code, complexity and when each beats std::sort.
Bucket sort groups values into range-based buckets, sorts each and joins them. See modern C++ code for doubles and integers, with complexity.
Radix sort orders integers digit by digit without comparisons. See a tested C++20 base-256 implementation, its complexity and when it beats std::sort.
Bubble sort is O(n^2) and simple; quicksort averages O(n log n). See C++20 code for both and why std::sort is preferred in real projects.
Bubble sort swaps adjacent out-of-order items until the array is sorted. See basic and templated C++ versions, complexity and better options.
Selection sort swaps the minimum into place; insertion sort shifts elements into a sorted prefix. See C++20 templates, stability and std::sort alternatives.
Insertion sort in C++ grows a sorted prefix by shifting larger items right. See vector and template versions, complexity and when std::sort is better.
See how selection sort works in C++ with vector and generic iterator implementations, its O(n²) complexity and why std::sort is preferred.
Compare merge sort and quick sort in C++ by time complexity, stability and memory, with working code and guidance on std::sort versus std::stable_sort.
Learn how quick sort works in C++ with a generic iterator-based C++20 implementation, complexity analysis, and when to use std::sort instead.
Merge sort is a stable O(n log n) divide-and-conquer algorithm. Learn how it works and a short generic C++ implementation built on std::merge.
Binary search finds a value in a sorted array in O(log n) time. See a safe C++20 implementation and when to use the standard library functions.
A hash table stores key-value pairs with hashing, while a symbol table tracks identifiers and scopes in a compiler. See the difference with C++ code.