I'm new to Python and I'm struggling with sorting numbers in a file. I want to do a bubble or an insertion sort and arrange the numbers in the file in ascending order. The numbers are not integers. This is what I have so far:
input_file=open("C:\\Users\\Rachel\\Documents\\Natural Sciences MSci\\Year 2\\Env Sci\\Comp Modelling\\Pptn data.txt", "r")
header_line = input_file.readline()
for line in input_file:
print line
list=input_file.read()
print list
def insertion_sort(items):
for i in range(1, len(items)):
j=i
while j>0 and items[j] < items[j-1]:
temp=items[j]
items[j]=items[j-1]
items[j-1]=temp
j=j-1
insertion_sort(list)
print 'After sorting:', list
After I run this, the unsorted list is printed and the phrase After sorting:appears but without the sorted list of numbers :D
I'm sure I'm missing something obvious but I've tried loads of different ways and can't seem to get it.
Any help would be great Thanks!
while j>0 and items[j] < items[j-1]:, I think. If the second condition is not met, which is a very normal case, you will not get into the loop and so j will not be decremented.