Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language is Python. Using these imports. The last one is from anaconda. from time import time import matplotlib . pyplot as plt DynamicArray: import ctypes

image text in transcribed

Language is Python. Using these imports. The last one is from anaconda.

from time import time import matplotlib.pyplot as plt 

DynamicArray:

import ctypes class DynamicArray(object):   def __init__(self):  self._n = 0  self._capacity = 1  self._A = self._make_array(self._capacity)   def _make_array(self, c):  return (c * ctypes.py_object)()  # creates an "array" of pointers   def __len__(self):  return self._n def __getitem__(self, item):  if not 0 item self._n:  raise IndexError('invalid index')  return self._A[item]   # GROW THE ARRAY DYNAMICALLY  def _resize(self, c): # c should be larger that the original array  B = self._make_array(c)  for k in range(self._n):  B[k] = self._A[k]  self._A = B self._capacity = c   def append(self, obj):  if self._n == self._capacity: # list is full  self._resize(2 * self._capacity) # grow the "list" by twice its size  self._A[self._n] = obj  self._n += 1   # Insert... see mutating  # assume 0  def insert(self, k, value):  if self._n == self._capacity: # out of empty cell, must make room  self._resize(2*self._capacity) # double capacity  for j in range(self._n, k, -1): # shift rightmost first; -1 iterates through the range backwards  self._A[j] = self._A[j-1]  self._A[k] = value  self._n += 1   # Pop the last element of the array  def pop(self):  # do not shrink the array; only allows removal of last element  if self._n > 1:  self._A[self._n - 1] = None # help garbage collection  self._n -= 1 # one less element  return 
Consider an implementation of a dynamic array, but instead of copying the ele- ments into an array of double the size (that is, from N to 2N) when its capacity is reached, we copy the elements into an array with N/Al additional cells, going from capacity N to capacity N LN/11. rove that performing a sequence of n append operations still runs in O(n) time in this case. Note: Prove this experimentally, by showing the O(n) trend graphically. You must first fix the implementation of DynamicArray. py to account for the new capacity resize. Notice that the capacity is incremented with the ceiling function on N/4. You must submit your implementation of DynamicArray.py to receive credit for this

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

More Books

Students also viewed these Databases questions

Question

Explain the guideline for job description.

Answered: 1 week ago

Question

What is job description ? State the uses of job description.

Answered: 1 week ago

Question

What are the objectives of job evaluation ?

Answered: 1 week ago

Question

Write a note on job design.

Answered: 1 week ago