Question: python help insertion sort i am having trouble getting my results to display in an output file, they display to the screen correctly. any help

python help insertion sort

i am having trouble getting my results to display in an output file, they display to the screen correctly. any help is appreciated. code below

def insertion_sort(sort_list):

for i in range(1, len(sort_list)):

key = sort_list[i]

j = i-1

while j>=0 and key < sort_list[j]:

sort_list[j + 1] = sort_list[j]

j -= 1

sort_list[j + 1] = key

print(' The sorted list: \t', sort_list)

print(' ')

f=open("input.txt","r"); #input array prior to sort

o=open("ouput.txt", "w"); #output array file after sort

list1=f.readline().split();

list2=f.readline().split();

l1=[]

l2=[]

for val in list1:

l1.append(int(val))

del l1[0]

print("Insert Sort");

l1 = insertion_sort(l1)

print(l1)

o.write(str(l1) + ' ')

for val in list2:

l2.append(int(val))

del l2[0]

l2 = insertion_sort(l2)

print (l2)

o.write(str(l2) + ' ')

f.close()

o.close()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!