卷別
資料結構與演算法
申論國立臺灣大學 2023 · 考試 · 第 15 題
考科演算法
(21 points) Given singly linked lists, each of which has nodes. The numbers in the nodes of the -th list are given by . Each of the lists has the numbers sorted in non-decreasing order, i.e., . Professor Q asks the students to develop an algorithm to merge these lists into one singly linked list sorted in non-decreasing order. Three students have come up with different algorithms. For each of these three algorithms, analyze and give their asymptotic worst-case running times with big-O notation and in terms of and (tight bound; drop lower-order terms and constant factors other than and ).
- Student A: Repeatedly iterate over the numbers in the head nodes of the lists, find the node with the smallest number, remove from its original list and append it to the tail of result list ; until all lists are empty.
- Student B: Create an empty min heap . Remove the head nodes and insert them into (each node also carries the index of its source list). Repeatedly remove the min node from , append it to , and (if its source list is not empty) remove the head of list and insert into ; while is not empty.
- Student C: Recursive divide-and-conquer
MergeTwoGroupLists: if only one list, return it; otherwise split the input lists into two groups of and lists, recursively merge each group into one sorted list , then merge the two sorted lists into one.