Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions