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)
Your code has just one minor flaw. As @Tomerikoo pointed out already, there is just a little mistake at the iterators. The correct code would look like this:
There is a bit more elegant implementation, that iterates over the list items instead of just the indices.
In the one case where you actually need an index an
enumerateis your tool of choice. This improvement makes your code easier to read in general and utilizes one of the features of Python instead of, for example, C.