Do your own homework
it's not my homework I'm doing it from MIT's open course wear.
It's trivial. All you need is to maintain a sorted queue of the two biggest numbers found so far, where the queue is represented by the variables biggest and second_biggest. So if you find something bigger than biggest, you shift the whole queue and if you find something bigger than just the second record in the queue, you just replace this record.
General algorithms for N-th number out of M are way more tricky.
> way more tricky
I disagree. It still lends itself easily to a straightforward constant-space algorithm. The only complexity is that the code no longer can be done with ad-hoc variables, so it pretty much forces you to handle the top-n subset of your input data as a sorted array. (Which, pedagogically speaking, is a very good thing. Variables like "fifth_biggest" give me nervous tics.)
You're basically sorting the input, and pruning the sorted list as you go along. The queue is never going to exceed n items, and after the input is exhausted, you pluck the bottom (nth) item from the queue. You could do a modified insert/merge-sort algorithm with a "falling-off" effect. It involves, instead of manually handling the individual elements, making a separate array to store the top N elements of your incoming data.
>>5
You should be putting your highest m numbers in a binary search tree. Then for each number: 1. you insert it in the BST. and 2. remove the (m+1)th smallest element in the tree.
So you would get a O(n log m) algorithm instead of a O(nm) algorithm, where n is the number of elements in the array, and m is from the mth largest element that you want.