The algorithm works as follow:
- I find the minimum of a given list.
- Then pop it and append it to another list.
- Repeat step 1 and 2 until there is one element, in which case i simply append it to the other list and end the program.
Issue
The last element is always some random number which should be sorted long ago.
Source Code
lst=[randrange(1, 100) for i in range(100)]
lst2=[]
while True:
if len(lst) > 1:
min = 0
for i in range(len(lst) -1):
if min == 0:
min = lst[i]
else:
if lst[i] < min:
min = lst[i]
for j in range(len(lst) -1):
if lst[j] == min:
lst2.append(lst[j])
lst.pop(j)
break
else:
lst2.append(lst[0])
break
lst = lst2
print(lst)
range'sendargument is exclusive. So it should just befor i in range(len(lst)):list.popreturns the popped element, so it can just be:lst2.append(lst.pop(j))indexto get the index without explicitly iterating andminto get the minimum value in the same manner - it'll give you far more readable code.lst.sort()...