These sort of problems are much more amenable to understanding if you first write out your algorithm either mathematically, or in many cases, in plain language pseudo-code. Example for an insertion sort (your final code is not using an insertion sort, because it is sorting after the array is generated), you generate a number, and insert it into the list. The insert function will find the correct position and if it is not at the bottom, move everything below where it goes down one element, and put the new member there. So, in plain language pseudo-code:
main:
while not done
generate number N
insert N in list
check if done
end while
insert N:
# First do empty list optimization
if list size == 0
list[0] = N
list size = 1
else
for i = 0, while insert not done and i < list size, i = i + 1
if list[i] > N # we found the spot
move list from i to list size - 1 down one element
list[i] = N
insert done = true
else if i == list size - 1 # We are at end of list, N goes here
list[list size] = N
list size = list size + 1
insert done = true # this is redundant because the increment at the
# top of the loop will kick us out, but say what you mean
# anyway, it will eliminate an entire class of bugs
end if
end for
end if
I'm leaving the move list down function to you... :-)