Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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.

Expert Solution
Check Mark
Step 1

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)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education