Example 1 — Task queue by priority
Higher numbers run first.
#include <stdio.h>
struct Task { const char *name; int pri; };
int main(void) {
struct Task q[] = {
{"write report", 2}, {"fix bug", 5}, {"lunch", 1}, {"deploy", 4}
};
int n = 4;
while (n > 0) {
int best = 0;
for (int i = 1; i < n; i++)
if (q[i].pri > q[best].pri) best = i;
printf("%s (priority %d)\n", q[best].name, q[best].pri);
q[best] = q[--n];
}
return 0;
} How It Works
- Each round scans for the largest priority.
- The chosen task is removed by overwriting it with the last element.
- Tasks leave in priority order: 5, 4, 2, 1.
