← 回題庫

卷別

資料結構與演算法

選擇

國立臺灣大學 2024 · 考試 · 第 3 題

(5 points) A Dynamic Array (abbreviated as DArray) is a type of array that permits the insertion and deletion of elements at the end to dynamically adjust its size. DArray DD maintains three crucial attributes, including the underlying array D.dataD.\text{data}, the logical data size D.sizeD.\text{size}, and the actual array capacity D.capacityD.\text{capacity}. The invariant D.size≤D.capacityD.\text{size} \le D.\text{capacity} holds at any given moment. The Insert(D,x)\text{Insert}(D, x) operation appends the element xx to the end of D.dataD.\text{data}. In case D.size=D.capacityD.\text{size} = D.\text{capacity}, it triggers a call to Resize(D)\text{Resize}(D) before appending xx, where Resize(D)\text{Resize}(D) allocates a new and larger array, moves all the data to it, increases the value of D.capacityD.\text{capacity}, sets up D.dataD.\text{data}, and finally frees the old array. Two implementations of Resize(D)\text{Resize}(D) are provided: ResizeA(D)\text{ResizeA}(D) increases D.capacityD.\text{capacity} by a constant (e.g., 10) while ResizeB(D)\text{ResizeB}(D) doubles D.capacityD.\text{capacity} each time.

To initialize DArray DD, we set D.sizeD.\text{size} to 0, D.capacityD.\text{capacity} to 1, and allocate a size 1 array to D.dataD.\text{data}. Assuming that both allocating and freeing arrays of length nn take O(n)O(n) time, and moving a single element takes O(1)O(1) time, please select the correct description(s) below.

  • (A)
    DArray is not suitable for random access because its memory may be relocated.
  • (B)
    DArray can only be allocated in the heap memory due to its dynamic memory nature.
  • (C)
    If ResizeA(D)\text{ResizeA}(D) is considered, the amortized time complexity of Insert(D,x)\text{Insert}(D, x) is O(1)O(1).
  • (D)
    If ResizeB(D)\text{ResizeB}(D) is considered, the amortized time complexity of Insert(D,x)\text{Insert}(D, x) is O(1)O(1).
  • (E)
    In the Delete(D)\text{Delete}(D) operation, if D.size>0D.\text{size} > 0, D.sizeD.\text{size} is decreased by one. If, at this stage, D.size<⌈D.capacity/2⌉D.\text{size} < \lceil D.\text{capacity}/2 \rceil, we create a new array d′d' with a size of ⌈D.capacity/2⌉\lceil D.\text{capacity}/2 \rceil, transfer the data to d′d', set up d′d' as the new data array for D.dataD.\text{data}, and subsequently free the old array. Then, the amortized time complexity of mixed Insert and Delete operations can be O(1)O(1), where Resize(D)\text{Resize}(D) can be either ResizeA(D)\text{ResizeA}(D) or ResizeB(D)\text{ResizeB}(D).

標準解析

AI 已複查