0

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]

4
  • provide sample input, actual output and expected output Commented May 18, 2016 at 5:33
  • this is only making one pass through the list ... you need to continue until you make no swaps ... also you can swap in python with vals[x],vals[y]=vals[y],vals[x] Commented May 18, 2016 at 5:33
  • Are you required to implement a sorting algorithm? Because the practical answer would be to use the built-in Python sorting method(s). Commented May 18, 2016 at 5:36
  • We have to make our own sorting algorithm, but it can resemble a premade algorithm. I'm basing this off of selection sort I think. How is it only making one pass? Commented May 18, 2016 at 5:38

4 Answers 4

1

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] # <--
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! that did it! How would I incorporate curr, butt update accordingly?
@DChalo according to your code, curr is the value of vals[x], so whenever vals[x] changes, update curr accordingly.
1

Change this part of code

 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;

To this:

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

1 Comment

Sweet! Thank you so much
0

You can solve sorting of lists much easier and in a more pythonic way, simply use the sort command, here is an example:

import random

a=random.sample(range(30), 10)
print a
a.sort()
print a

1 Comment

Thanks! It's just for this problem we couldn't use a built in algo
0

You're welcome -

numCol=int(input());
vals=[];
for x in range(numCol):
    vals.append(int(input()))

for x in range(len(vals)):
    for y in range(x+1,len(vals)):
    if(vals[x]>vals[y]):
        temp=vals[y]
        vals[y]=vals[x]
        vals[x]=temp
print(vals)

The problem was that you weren't changing the value of cur to be compared correctly.

Edit - Just saw other answers had beat me to it :(

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.