It seems like the best complexity would be linear O(n).
Doesn’t matter the case really, I’m speaking of greedy algorithms in general.
Sometimes it pays off to be greedy?
In the specific case that I am interested would be computing change.
Say you need to give 35 cents in change. You have coins of 1, 5, 10, 25. The greedy algorithm, coded simply, would solve this problem quickly and easily. First grabbing 25 cents the highest value going in 35 and then next 10 cents to complete the total. This would be best case. Of course there are bad cases and cases where this greedy algorithm would have issues. I’m talking best case complexity for determining this type of problem.
Any algorithm that has an output of
nitems that must be taken individually has at bestO(n)time complexity; greedy algorithms are no exception. A more natural greedy version of e.g. a knapsack problem converts something that is NP-complete into something that isO(n^2)–you try all items, pick the one that leaves the least free space remaining; then try all the remaining ones, pick the best again; and so on. Each step isO(n). But the complexity can be anything–it depends on how hard it is to be greedy. (For example, a greedy clustering algorithm like hierarchical agglomerative clustering has individual steps that areO(n^2)to evaluate (at least naively) and requiresO(n)of these steps.)