0

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!

2
  • Also note, that you probably want to use file.readlines() instead of file.read(), to read the lines of the file into a list. Commented Feb 27, 2016 at 13:09
  • You will have to split 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. Commented Feb 27, 2016 at 13:24

3 Answers 3

1

One problem is that the initial for loop exhausts the data from the input file so that there is nothing left to read in the subsequent input_file.read(). Also read() will return a string, not a list. But in any case your insertion sort function is operating on an empty string, so it does nothing at all.

You could fix the first problem by seeking to the beginning of the file after the for loop. The second problem can be fixed by splitting the input by lines using splitlines():

header_line = next(input_file)
for line in input_file:
    print line

input_file.seek(0)
next(input-file)    # skip header again
list=input_file.read().splitlines()
print list

But it's probably better to just do this:

with open('input_file') as input_file:
    header_line = next(input_file).strip()
    numbers = [line.strip() for line in input_file]
    # if you really want to print them out first...
    for number in numbers:
        print number

    insertion_sort(numbers)

N.B. this code does not convert the data from the file into any numeric type (e.g. integer), because you said that the numbers are not integers... so what are they? Not converting to a numeric type means that your sort function will sort based on the ASCII collating sequence of the numeric strings, so '10' would sort before '2'.

If the numbers can be floats, you can do this when reading the file:

numbers = [float(line) for line in input_file]

now your sort function will sort numbers such as 1 or 1.0 as floats.

Sign up to request clarification or add additional context in comments.

Comments

0

Sorry for the confusion with what your goal was. Here is the correct code:

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

list=input_file.read().split()


def insertion_sort(items):
    for i in range(1, len(items)):
        j = list[i]
        i = i - 1
        while i >= 0:
            if j < list[i]:
                list[i + 1] = list[i]
                list[i] = j
                i = i - 1
            else:
                break


insertion_sort(list)
print 'After sorting:', list

Comments

0

Your algorithm seems to work just fine. I tried the following on my computer. I created a file called numbers.txt and placed the numbers in the following manner:

23
23.4
4
5
6.7
1
0
6
34

And then ran the following code:

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

numbers = open("numbers.txt").read().split()
numbers = [float(number) for number in numbers]
print "Before sorting: ", numbers
insertion_sort(numbers)
print "After sorting: ", numbers

Which gave me the following output:

Before sorting:  [23.0, 23.4, 4.0, 5.0, 6.7, 1.0, 0.0, 6.0, 34.0]
After sorting:  [0.0, 1.0, 4.0, 5.0, 6.0, 6.7, 23.0, 23.4, 34.0]

I hope this helps.

2 Comments

The problem is not with the sorting algorithm, it is in the code that reads the file.
@mhawke Oh, I didn't realize that you had already posted the same. Hadn't refreshed the page. Sorry about that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.