i have the following problem We have a set S of n potential investments, each given by a pair of floating point numbers (amount, estimated return) There is a total amount A to invest; we want to select investments to maximise the return on this amount. please explain how do i order the investments using the ratio estimated return/amount, in nlogn. is this using quick sort? i can calculate the ratios in O(n) then how do i keep an index as to which ratio belongs to which investment? am trying to solve it using the following algo 1.order the investments according to the decreasing ratio r/a 2.allocate available money to the investments in such order. I.e. buy all the first investment, then use available money to buy the second investment, then the third, etc, until the money is finished. asked 19 Mar '13, 14:00

Hello, I guess this is a DP problem, but your sorting idea is also not too bad... What you have is a compound data type, called investment, that has two "members" associated with it: an amount and an estimated return. And you want, as you clearly pointed out, sort them by a given ratio... What you can do, is to use a comparison function devised specifically to compare investiments: You compare two investments by their ratios and sort them accordingly: In C++ you can do something like:
And then you can use your newly defined cmp function as a third argument for the builtin std::sort function and you'll have an array with the ordered investments :D Below you can find a complete example:
Best regards, Bruno answered 19 Mar '13, 15:44
