I have a list of unspent outputs. Let's say: 0.4 BTC, 0.6 BTC and 1.0 BTC. Now I want to create a new transaction for a 0.5 BTC. How I understand it, it would be best to take the 0.6 BTC output as input, and add 0.1 output as change. The simpler way would be to just start at the beginning and add up inputs until I reached the desired amount. But that seems stupid...
How is this being done by the usual bitcoin clients? What's the best way of finding the most optimal unspent outputs?