Knapsack Problem in C and C++: 0/1 Dynamic Programming, Item Recovery and the Fractional Variant
0/1 knapsack in C and C++: the DP recurrence, item recovery, why greedy and an upward loop give wrong answers, and the fractional variant, all tested.
Algorithms are the foundation of all software — the precise, step-by-step instructions that tell a computer how to solve a problem efficiently. Understanding algorithms is not just an academic exercise. It is what separates programmers who write code that works from engineers who write code that works fast, scales under load and handles complex problems elegantly. From sorting a list of names to finding the shortest path across a network, algorithms determine how well your software performs in the real world.
MYCPLUS has been publishing algorithm tutorials and implementation guides since 2004, with a particular focus on practical algorithm implementations in C and C++. This section covers the essential algorithms every programmer should understand — sorting, searching, graph traversal, tree operations and optimisation techniques — alongside the data structures those algorithms are built on, each explained and implemented in code you can compile and study.
Each tutorial explains how the algorithm works, analyses its time and space complexity, and includes working source code you can compile and study. The refreshed tutorials go further: the code is compiled and run for the article, its output is shown as captured, and the source and tests are on GitHub. Whether you are preparing for technical interviews, studying computer science fundamentals or optimising production code — start here.
Why MYCPLUS: Trusted by programmers and computer science students since 2004 — the algorithm tutorials pair each explanation with complexity analysis and source code, and the refreshed ones are compiled, run and tested for the article.
0/1 knapsack in C and C++: the DP recurrence, item recovery, why greedy and an upward loop give wrong answers, and the fractional variant, all tested.
A square-root primality test, the Sieve of Eratosthenes and Miller–Rabin in seven languages, with the four bugs that pass a quick test.
At n = 1,000,000, O(log n) takes 20 steps and O(n squared) takes a trillion. The ten families that matter, with complexity for each.
Four ways to compute Fibonacci numbers in C and C++, with measured call counts and the exact term at which each integer type stops being correct.
Kruskal’s algorithm builds a minimum spanning tree by sorting edges and rejecting cycles with union-find. Tested C, C++, and Python code included.
Coding competitions are the fastest way to sharpen your problem-solving skills. Here are the best competitive programming platforms and contests to enter in 2026, for every level.
In a lot of programs my gensort-utility turned out to be very useful. It consists simply of the header file
The big sort routine implements a way to sort huge amounts of data using C programming language. It sorts the data that do not fit into main memory by using a multi-phase sorting on files. It is a implementation from the book “Algorithms and data structures” by Niklaus Wirth. Additionally, this routine recognizes small amounts of data that do fit into memory and resorts to a in-place quicksort.
Stanford Engineering Everywhere (SEE) offers few computer science courses to students online and at no charge. Programming Abstractions course covers advanced programming topics such as recursion, algorithmic analysis, and data abstraction using the C++ programming language, which is similar to both C and Java.
Delve into the fundamentals of data structures and algorithms with Data Structures and Algorithm Analysis in Java. This comprehensive guide focuses on performance optimization, scalability, and real-world Java implementations. From sorting to dynamic programming, it equips developers with the tools to build efficient, high-performance applications. A must-read for students and professionals seeking to master Java’s capabilities in solving complex computational problems.
Master efficient coding with Data Structures and Algorithm Analysis in C++ – your ultimate guide to building high-performance software solutions!
These algorithms are represented in terms of compact implementations in C so that readers can both understand their fundamental properties and implement them in real applications. The method of the analysis of algorithms is quite carefully designed. When appropriate, analytic results are described to explain why certain algorithms are preferred. In some cases, the practical algorithms’ relationship being discussed to purely theoretical results is also described.