Hi guys, I have written this program so far which reads and sorts numbers from a file...WITHOUT the sort function. It sorts from largest to smallest right now.
My Question:
How do I make my program list the numbers from smallest to largest instead?
File contains the following:
2 1 5 10 8
3 900
47 6 11
Program:
scores = [0,0,0,0,0,0,0,0,0,0]
result = [0,0,0,0,0,0,0,0,0,0]
file = open("test.txt","r")
lines = file.readlines()
file.close()
count = 0
result_index = 0
largest = 0
if (lines):
for line in lines:
s = line.split()
for score in s:
scores[count] = int(score)
count = count + 1
while (result_index < count):
smallest = scores[9]
for i in range(0,count):
if scores[i]+1 > smallest:
smallest = scores[i]
index_of_smallest = i
result[result_index] = smallest
scores[index_of_smallest] = largest + 1
result_index = result_index + 1
else:
print "no data in the file!"
print "result is ",result
shell output:
result is [900, 47, 11, 10, 8, 6, 5, 3, 2, 1]