DChalo - 1 year ago 71

Python Question

So i'm supposed to take in a number n, add n numbers to a list, then sort the list and print.

`numCol=int(input());`

vals=[];

for x in range(numCol):

vals.append(int(input()))

for x in range(len(vals)):

curr=vals[x];

for y in range(x+1,len(vals)):

if(curr>vals[y]):

temp=vals[y];

vals[y]=curr;

vals[x]=temp;

print(vals);

the code doesn't work properly. We haven't learned sorting algorithms thoroughly really yet, so i'm kinda just making my own, but it resembles selection sort I think. Anyways, why is it not printing the values in ascending order?

Edit: I input 4, then make list 4, 3, 2, 1. Output is [1, 4, 4, 4]

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

the problem is `curr`

should change after swap.

```
for x in range(len(vals)):
curr=vals[x];
for y in range(x+1,len(vals)):
if(curr > vals[y]):
temp = vals[y]
vals[y] = curr
vals[x] = temp
curr = vals[x] # <--
```