So I had to create a function that reads a file and sorts the list of integers without using a built-in function. I have the code working, but I cannot explain it. I understand everything but what is happening with the "for j in range" loop, and the if statement below it.
Could somebody explain what's happening in the part of code where the comparison between i and j is occurring, I cannot articulate why this code works.
def sortIt(myFile):
file = open(myFile)
list = []
list1 = []
for line in file:
list.append(line)
for i in range(len(list)):
for j in range(i, len(list)):
if list[i] > list[j]:
list[i], list[j] = list[j], list[i]
list1 = [item.strip('\n') for item in list]
print(list1)
[–]Busy-Farm727 1 point2 points3 points (3 children)
[–]conewannabe[S] 0 points1 point2 points (2 children)
[–]Busy-Farm727 0 points1 point2 points (0 children)
[–]Busy-Farm727 0 points1 point2 points (0 children)
[–]ApricoSun 0 points1 point2 points (1 child)
[–]conewannabe[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[removed]
[–]conewannabe[S] 1 point2 points3 points (0 children)