Pseudocode only since it's homework, to be turned into your language of choice. Assuming the list has two or more numbers in it (indexes 0 and 1):
set lowest to value at index 0.
set second_lowest to value at index 1.
if lowest is greater than second_lowest:
swap lowest and second_lowest.
vary idx from 3 to last element:
if value at index idx is less than lowest:
set second_lowest to lowest
set lowest to value at index idx
else
if value at index idx is less than second_lowest:
set second_lowest to value at index idx
This works by basically checking every number to see if it should be the new lowest or new second lowest number. It can be done in one loop.
What you want to do is to run this program in your head, writing down on paper what the variables get changed to. That's a good way to understand how a computer works. Consider the list [30,20,90,10,50,12,7]
, following the following steps:
lowest second description
------ ------ -----------
30 20 store first two elements.
20 30 swap them if in wrong order (they are).
20 30 90 is not less than either so ignore.
10 20 10 is less than lowest (20) so move
lowest to second, store 10 to lowest.
10 20 50 is not less than either so ignore.
10 12 12 is less than second (20) so
store 12 to second.
7 10 7 is less than lowest (10) so move
lowest to second, store 7 to lowest.