
Python help. Using ( Heapq ) sorting algothrim create only one function def sortstring(filename): that takes in a file which has a bunch of sentences, each line of sentence consting of different length. Sort the setences from the lower length senteces to the highest length sentence. Print it after ( DON'T USE ANY SORTING BULT-IN FUNCTION ).
Note: Keep note of time complexity as it should be able to run few 10,000 lines of sentences in few seconds.

code-
filename = input()
def sortstring(filename):
o=open(filename, "r")
Lines = o.readlines()
li=[]
d={}
for i in Lines:
l=len(i.split())
d[l] = i
a = list(d.keys())
lis=heapSort(a)
for i in lis:
print(d[i])
def heapify(a, n, i):
lar = i
l = 2 * i + 1
r = 2 * i + 2
if r < n and a[lar] < a[r]:
lar = r
if l < n and a[i] < a[l]:
lar = l
if lar != i:
a[i],a[lar] = a[lar],a[i]
heapify(a, n, lar)
def heapSort(a):
n = len(a)
for i in range(n, -1, -1):
heapify(a, n, i)
for i in range(n-1, 0, -1):
a[i], a[0] = a[0], a[i]
heapify(a, i, 0)
return a
sortstring(filename)
Step by stepSolved in 3 steps with 2 images

- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





