Algorithm 3 Merge Sorted Lists

Input: kk sorted lists with nn elements in total

Output: A sorted list of size nn

1:extract the first element from each sorted list to form A[1..k]A[1..k]

2:A.heap-size=kA.\text{heap-size} = k

3:BuildMinHeap(AA)

4:let LL be the result list

5:while A.heap-size0A.\text{heap-size} \ne 0 do

6:x=x = HeapExtractMin(AA)

7:let ll be the list which xx came from

8:if ll is not empty then

9:extrace the first element yy from ll

10:MinHeapInsert(A,yA, y)

11:end if

12:append xx to LL

13:end while

14:return LL