You are on page 1of 2

Homework - Week 11

Name:RAHMAN MD MATIUR
Student Id :2230130236
Please watch these videos first, and finish your homework after class. You can edit
WORD documents directly, or take photos after writing them down, and send them to my
email. My email: xupeng.01@163.com
If you can't do some problems in your homework, you can leave it blank.
请大家先认真学习视频,再完成下列作业。作业可以直接编辑 Word 文档,也可
以写下后拍照,将文档或照片发我的电子邮箱。我的邮箱:xupeng.01@163.com
如果作业中有些题目不会,可以空着不做。

Task 1: Lists the running time of each operation of the dynamic array. (列出动态数组各
个操作的运行时间)
Answer:

The running time of each operation of a dynamic array can vary depending on the
specific implementation and programming language used. However, here are the typical
time complexities for common operations:

Accessing an element by index: O(1)

Appending an element to the end of the array: O(1) amortized (assuming that the array
has enough capacity to accommodate the new element; otherwise, it may take O(n) time
to allocate a new array and copy the existing elements)

Inserting an element at a specific index: O(n) in the worst case, where n is the number
of elements in the array (since all elements after the insertion point may need to be
shifted to make room for the new element)

Removing an element from the end of the array: O(1)

Removing an element from a specific index: O(n) in the worst case (since all elements
after the removal point may need to be shifted to fill the gap)

Doubling the capacity of the array: O(n), since all existing elements need to be copied to
the new, larger array.

Note that these time complexities assume that the size of the array is known in advance
and that resizing is done by doubling the capacity of the array when necessary. If the
array needs to be resized more frequently, the time complexity of appending and
inserting elements may be worse.

Task 2:Which three methods are used to calculate amortized cost?(哪三种方法计算摊


余成本?)
Answer:
The three methods commonly used to calculate amortized cost are:

Straight-line method: In this method, the amortization expense is spread equally over the
useful life of the asset. For example, if an asset costs $10,000 and has a useful life of 5 years,
the straight-line method would allocate $2,000 of the cost to each year.

Declining balance method: This method involves applying a fixed percentage rate to the
remaining balance of the asset each year. This results in a higher amortization expense in the
early years of the asset's life and lower expense in later years. For example, if an asset costs
$10,000 and has a useful life of 5 years, and a depreciation rate of 40%, the first year's
amortization expense would be $4,000, the second year's expense would be $2,400, and so on.

Units of production method: This method allocates the cost of the asset based on its usage or
output. For example, if an asset costs $10,000 and is expected to produce 100,000 units over
its useful life, the amortization expense would be calculated based on the number of units
produced each year. If 20,000 units are produced in the first year, then the amortization
expense for that year would be $2,000.

Task 3:Talk about the difference between priority queue and ordinary queue.(讲一讲优
先队列与普通队列的区别。)
Answer:
A priority queue and an ordinary queue are two types of data structures used in
computer science to manage collections of elements. However, they differ in the way
they prioritize and access elements from the queue.

An ordinary queue is a first-in, first-out (FIFO) data structure that stores elements in
the order in which they are added to the queue. In an ordinary queue, the element that
is added first is the first one to be removed. The operations supported by an ordinary
queue include adding an element to the end of the queue (enqueue) and removing an
element from the front of the queue (dequeue).

On the other hand, a priority queue is a data structure that stores elements in an order
determined by their priority. In a priority queue, the element with the highest priority is
always at the front of the queue and is the first one to be removed. The priority can be
assigned based on any criteria, such as numerical value, time stamp, or any other
custom rule. The operations supported by a priority queue include adding an element
with a priority (enqueue) and removing the element with the highest priority (dequeue).

To summarize, an ordinary queue is a FIFO data structure, while a priority queue is a


data structure that prioritizes elements based on some criteria.

You might also like