0

I'm new to python and I'm trying to write a selection sort code starting with the last digit and eventually working its way to the first but I'm getting an error that the index is out of range... anyone know what's wrong with my code?

inputfilename = raw_input("What is the name of the file to be sorted? ")

print("Warning the outputfile will be overwritten if it exists!")

outputfilename = raw_input("Input the name for the output file: ")

inputfile = open(inputfilename)

outputfile = open(outputfilename, 'w')

numberlist = map(int, inputfile)

i = (len(numberlist))

while ( i > int(1)):
m = i
j = i - 1
while (j > int(0)):
if (numberlist[j] > numberlist[m]):
m = j
j = j - 1
temp = numberlist
numberlist = numberlist[m]
numberlist[m] = temp
i = i -1
for number in numberlist:
outputfile.write(str(number) + "\n")
outputfile.close()

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by pyTony
0

Maybe because you set m = len(numberlist) and indexes of numberlist goes until len(numberlist) - 1 (and start from zero).

With code tags your code would be much nicer to read.

Edited by pyTony: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.