THINK FIRST·CODE LATER

← All labs

Merging two sorted lists

Problem

Two class sections each hand in a list of scores already sorted in increasing order. MergeSorted reads the first list (its length, then its values) and the second one in the same way, and merges them into one sorted array without sorting again: repeatedly copy the smaller of the two front elements.

Write readList, isSorted and merge(int[] a, int[] b). If a list is not sorted, print Both lists must be sorted. For the input

5
1 5 16 61 111
4
2 4 5 6

the program prints

Merged: 1 2 4 5 5 6 16 61 111
Length: 9
Still sorted: true

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l9-8-data-1.txt

5
1 5 16 61 111
4
2 4 5 6
What your program must print for l9-8-data-1.txt
Merged: 1 2 4 5 5 6 16 61 111
Length: 9
Still sorted: true

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l9-8-data-1.txt

Windows PowerShell does not understand <:

Get-Content l9-8-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.