def interleave (listl, list2): 'this interleaves two already sorted lists' newlist = [] while len (listl) +len (list2) >0: if len (list1)==0: newlist.extend(list2) list2 = [] elif len (list2)==0: newlist.extend (list1) list1 = [] else: if listl [0]

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
def interleave(listl,list2):
''this interleaves two already sorted lists
newlist = []
while len(listl)+len (list2) >0:
if len (listl)==0:
newlist.extend (list2)
list2 = []
elif len (list2)==0:
newlist.extend(list1)
list1 = []
else:
if listl[0]<list2[0]:
newlist.append(listl.pop(0))
else:
else:
newlist.append(list2.pop (0))
return newlist
def mergesort (mylist):
if len (mylist) <= 1:
return (mylist)
midpoint == len (mylist) //2
firstpart = mylist[:midpoint]
secondpart
mylist [midpoint:]
sortedfirst = mergesort (firstpart)
sortedsecond = mergesort (secondpart)
full_list =
=
interleave (sortedfirst, sortedsecond)
return( full_list)
↓
Transcribed Image Text:def interleave(listl,list2): ''this interleaves two already sorted lists newlist = [] while len(listl)+len (list2) >0: if len (listl)==0: newlist.extend (list2) list2 = [] elif len (list2)==0: newlist.extend(list1) list1 = [] else: if listl[0]<list2[0]: newlist.append(listl.pop(0)) else: else: newlist.append(list2.pop (0)) return newlist def mergesort (mylist): if len (mylist) <= 1: return (mylist) midpoint == len (mylist) //2 firstpart = mylist[:midpoint] secondpart mylist [midpoint:] sortedfirst = mergesort (firstpart) sortedsecond = mergesort (secondpart) full_list = = interleave (sortedfirst, sortedsecond) return( full_list) ↓
(1) Use perf_count and the other ideas from 8.1 above to generate a list of data, where the ith entry is the
time tm(i) necessary for merge-sort to sort lists of randomly chosen elements of length 2¹ for i = 1, ..., 10.
(2) Plot tm(i) against 2¹ on a loglog plot. Briefly explain (as a comment) what the graph says about merge-
sort's speed, using big-O notation.
(3) Write a function insertionsort (somelist) that applies the insertion sort algorithm described in
lectures to sort a list. (You should use the pop and insert commands as described in the lecture notes.)
(4) Use this to find the times tỉ(i) for lists of the same length as in (1). Add tr(i) to your graph (on the same
axes as above), and write a comment on-what this says about the speed of insertion sort, in big O notation.
(5) Write a function countingsort (mylist) which implements counting sort for lists of non-negative
integers, using the algorithm described in lectures.
(6) How efficient is countingsort compared to the other algorithms you have investigated? Can you
provide one example each of lists of non-negative integers where counting sort would be (a) very efficient (b)
very inefficient? Write your answer as a comment.
Transcribed Image Text:(1) Use perf_count and the other ideas from 8.1 above to generate a list of data, where the ith entry is the time tm(i) necessary for merge-sort to sort lists of randomly chosen elements of length 2¹ for i = 1, ..., 10. (2) Plot tm(i) against 2¹ on a loglog plot. Briefly explain (as a comment) what the graph says about merge- sort's speed, using big-O notation. (3) Write a function insertionsort (somelist) that applies the insertion sort algorithm described in lectures to sort a list. (You should use the pop and insert commands as described in the lecture notes.) (4) Use this to find the times tỉ(i) for lists of the same length as in (1). Add tr(i) to your graph (on the same axes as above), and write a comment on-what this says about the speed of insertion sort, in big O notation. (5) Write a function countingsort (mylist) which implements counting sort for lists of non-negative integers, using the algorithm described in lectures. (6) How efficient is countingsort compared to the other algorithms you have investigated? Can you provide one example each of lists of non-negative integers where counting sort would be (a) very efficient (b) very inefficient? Write your answer as a comment.
Expert Solution
steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY