08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 ■ Searching, Sorting, and Merging

• determines whether it was found. addToList is given the location in which

to insert a new word. Words to the right of, and including, this location are

shifted to make room for the new word.

• In declaring a function prototype, some compilers allow a two-dimensional

array parameter to be declared as in char [][], with no size specified for

either dimension. Others require that the size of the second dimension must

be specified. Specifying the size of the second dimension should work on

all compilers. In our program, we specify the second dimension using the

parameter max, whose value will be supplied when the function is called.

9.8 Merge Sorted Lists

Merging is the process by which two or more ordered lists are combined into one ordered list. For

example, given two lists of numbers, A and B, as follows:

A: 21 28 35 40 61 75

B: 16 25 47 54

They can be combined into one ordered list, C, as follows:

C: 16 21 25 28 35 40 47 54 61 75

The list C contains all the numbers from lists A and B. How can the merge be performed?

One way to think about it is to imagine that the numbers in the given lists are stored on cards,

one per card, and the cards are placed face up on a table, with the smallest at the top. We can

imagine the lists A and B as follows:

21 16

28 25

35 47

40 54

61

75

We look at the top two cards, 21 and 16. The smaller, 16, is removed and placed in C. This exposes

the number 25. We have this:

21 25

28 47

35 54

40

61

75

271

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!