0

So this is supposed to be a sorting program, but for some reason, it is not sorting the file I'm giving, but just giving me straight numbers as it is. Any help would be appreciated. Thank you

filename=input('Enter file path:')
file = open(filename, 'r')
alist = [(line) for line in file.readlines()]
print(alist)

def selectionSort(alist):
    for index in range(0, len(alist)):
        ismall = index
        for i in range(index,len(alist)):
            if alist[ismall] > alist[i]:
                ismall = i
        alist[index], alist[ismall] = alist[ismall], alist[index]
    return alist 

2 Answers2

1

You didn't call the function!

def selectionSort(alist):
    for index in range(0, len(alist)):
        ismall = index
        for i in range(index,len(alist)):
            if alist[ismall] > alist[i]:
                ismall = i
        alist[index], alist[ismall] = alist[ismall], alist[index]

filename=input('Enter file path:')
file = open(filename, 'r')
alist = file.readlines()

# Call the function!
selectionSort(alist)
print(alist)

You've told Python what selectionSort means, but you haven't told it to sort anything. You need to call selectionSort(alist) to actually perform the sort.

Also, the order you want the list to be sorted in is most likely not the order you're telling Python to sort it in. alist is a list of strings, so you're telling Python to use lexicographic comparison to order the list. If it's supposed to be treated as, say, integers, you need to convert the data to integers:

alist = [int(line) for line in file]

(Also, since selectionSort modifies the list it operates on, it's best not to return the list. If you return it, it gives the impression that it creates a new, sorted list.)

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • it is giving me the same result, it doesn't sort from least to greatest – user3369517 Mar 02 '14 at 01:34
  • @user3369517: You're probably telling Python to compare things the wrong way. Answer expanded. – user2357112 Mar 02 '14 at 01:36
  • alist = [int(line) for line in file] i actually did try that before, but it didn't work because i added int there, i did again now, it is actually giving errors now, "invalid literal for int() with base 10: " – user3369517 Mar 02 '14 at 01:46
  • What's the actual data? Can you show the data you're trying to sort? – user2357112 Mar 02 '14 at 01:46
  • [45, 7, 5, 25, 24, 1] just that for now – user3369517 Mar 02 '14 at 01:47
  • @user3369517: Does the file actually contain brackets and commas? If so, you need to parse that with something like `ast.literal_eval` first. – user2357112 Mar 02 '14 at 01:49
  • nope, it doesn't contain brackets, i just put it there – user3369517 Mar 02 '14 at 01:50
  • @user3369517: You're going to want to inspect your data file, then. Make sure you're looking at the right file. Make sure it doesn't have any weird non-numeric characters, including Unicode stuff. Make sure it doesn't have anything besides your data. – user2357112 Mar 02 '14 at 01:53
0

You have simply defined a method. You need to call it from somewhere. I added to your code and tested it. It works. Here you go: The last two lines actually make a call to the code you created: (be careful about indentation)

alist = [42,54,2,45,6,125,32]
print(alist)

def selectionSort(alist):
    for index in range(0, len(alist)):
        ismall = index
        for i in range(index,len(alist)):
            if alist[ismall] > alist[i]:
                ismall = i
        alist[index], alist[ismall] = alist[ismall], alist[index]
    return alist

newList = selectionSort(alist)
print newList
sshashank124
  • 31,495
  • 9
  • 67
  • 76