Mergesort · Compiler

Mergesort

Count comparisons during the merge of two sorted runs. Whenever both runs still have values, compare their first unused values once and take the smaller one. On equality, take the value from the left run. Once either run is exhausted, copy the rest without comparing.

Input
Line 1 contains the lengths n and m (0 to 100 each). The remaining whitespace-separated integers are the n values of the first array followed by the m values of the second. Each value is from -1000 to 1000. Both arrays are sorted in nondecreasing order. The first array is the left run.
Output
Print the number of comparisons between front values.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…