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()

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.