Greedy search algorithm [C#]