Greedy Algorithms in C

Advanced
8 min read
Updated: Nov 2026
2 examples

Take the Best Choice Now

A greedy algorithm builds a solution by repeatedly taking the locally best choice and never reconsidering it. It is fast and simple, and correct only when the problem has the greedy-choice property. Greedy is optimal for activity selection and for coin change with canonical coin sets like 25, 10, 5, 1, but it fails for sets such as {4, 3, 1}: for 6 it picks 4 + 1 + 1 (three coins) while 3 + 3 uses two.

Remember
sort candidates by a rule (largest, earliest finish, ...)
for each candidate in order:
    if it is compatible: take it

Cheat Sheet

Keep these facts handy while you work with Greedy Algorithms in C in C.

TopicDetail
Greedy choiceBest local option
CorrectnessNeeds proof or tests
TimeUsually O(n log n) for sorting
ExamplesActivity selection, Huffman, Prim, Kruskal

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Dynamic Programming Basics in C.

Example 1 — Greedy coin change

Make 63 cents with 25, 10, 5, and 1.

C
#include <stdio.h>

int main(void) {
    int coins[] = {25, 10, 5, 1};
    int amount = 63, total = 0;

    for (int i = 0; i < 4; i++) {
        int n = amount / coins[i];
        amount -= n * coins[i];
        total += n;
        if (n) printf("%d x %d\n", coins[i], n);
    }
    printf("total coins: %d\n", total);
    return 0;
}

How It Works

  1. Two quarters cover 50, leaving 13.
  2. One dime leaves 3, no nickel fits, and three pennies finish it.
  3. For this coin set, taking the largest coin first is optimal.

Example 2 — Activity selection

Pick the maximum number of non-overlapping activities by earliest finish time.

C
#include <stdio.h>

int main(void) {
    int s[] = {1, 3, 0, 5, 3, 5, 6, 8, 8, 2, 12};
    int f[] = {4, 5, 6, 7, 9, 9, 10, 11, 12, 14, 16};
    int n = 11, last_end = 0, count = 0;

    printf("selected:");
    for (int i = 0; i < n; i++) {
        if (s[i] >= last_end) {
            printf(" (%d,%d)", s[i], f[i]);
            last_end = f[i];
            count++;
        }
    }
    printf("\ncount: %d\n", count);
    return 0;
}

How It Works

  1. The activities are already sorted by finish time.
  2. Each activity that starts at or after the last chosen finish is taken.
  3. Finishing as early as possible leaves the most room for later activities.

Edge Cases & Pitfalls

Check these before relying on Greedy Algorithms in C in real programs.

wrong

Assuming greedy always works

Coins {4, 3, 1} for 6 give 3 coins greedily but 2 optimally. Verify with brute force or DP.

sort

Wrong sort key

Activity selection needs earliest finish, not earliest start.

Key Takeaways

  • Best local choice.
  • Fast but needs proof.
  • Sort by the right key.

One line: A greedy algorithm builds a solution by repeatedly taking the locally best choice and never reconsidering it.

Frequently Asked Questions

An algorithm that makes the locally optimal choice at each step, hoping to reach a global optimum.
When a locally best choice prevents a better overall solution, as with coin sets like {4, 3, 1}.
Continue with "Dynamic Programming Basics in C" next. Solve overlapping subproblems once with a table, using stair climbing. The C Algorithm Techniques hub lists every topic in order.

Next: Dynamic Programming Basics in C

Solve overlapping subproblems once with a table, using stair climbing.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful