You can visually divide your code into two parts. The first part is for inserting the values. The second part sorts these values. So you have to cut the second part and insert at the place, where sorting should happen, I hope you will find this place ;)
Also think that you should replace i
in the second for
with something else, for example with k
Good luck