6.9 Merging Sorted ListsSometimes it may be necessary to take two sorted lists and merge them to create a new sorted list. Consider the following code segment: listA = [ 2, 8, 15, 23, 37 ] listB = [ 4, 6, 15, 20 ] newList = mergeSortedLists(listA, listB) print(newList) which creates two lists with the items ordered in ascending order and then calls a user-defined function to create and return a new list created by merging the other two. Printing the new merged list produces [2, 4, 6, 8, 15, 15, 20, 23, 37] Problem SolutionThis problem can be solved by simulating the action a person might take to merge two stacks of exam papers, each of which are in alphabetical order. Start by choosing the exam from the two stacks with the name that comes first in alphabetical order. Flip it over on the table to start a new stack. Again, choose the exam from the top of the two stacks that comes next in alphabetical order and flip it over and place it on top of first one. Repeat this process until one of the two original stacks is exhausted. The exams in the remaining stack can be flipped over on top of the new stack as they are already in alphabetical order and alphabetically follow the last exam flipped onto the new stack. You now have a single stack of exams in alphabetical order. A similar approach can be used to merge two sorted lists. Consider the illustration in Figure 6.9.1, which demonstrates this process on the sample lists created in the example code segment above. ![]() Figure 6.9.1: 'The steps for merging two sorted lists into a new sorted list.
The items in the original list are not removed, but instead copied to the new list. Thus, there is no "top" item from which to select the smallest value as was the case in the example of merging two stacks of exams. Instead, index variables are used to indicate the "top" or next value within each list. The implementation of the Program Listing
The process of merging the two lists begins by creating a new empty list and initializing the two index variables to zero. A loop is used to repeat the process of selecting the next largest value to be added to the new merged list. During the iteration of the loop, the value at This process is repeated until all of the values have been copied from one of the two lists, which occurs when After the first loop terminates, one of the two lists will be empty and one will contain at least one additional value. All of the values remaining in that list must be copied to the new merged list. This is done by the next two while loops, but only one will be executed depending on which list contains additional values. The position containing the next value to be copied is denoted by the respective index variable Run Time Analysis![]() merging sorted lists worst-case:
To evaluate the solution for merging two sorted list, assume
In both cases, the three loops are executed for a combined total of iterations. Since the statements performed by each of the three loops all require constant time, merging two lists can be done in time.
|