08.01.2023 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9

Searching, Sorting, and Merging

In this chapter, we will explain the following:

• How to search a list using sequential search

• How to sort a list using selection sort

• How to sort a list using insertion sort

• How to sort a list of strings

• How to sort parallel arrays

• How to search a sorted list using binary search

• How to merge two sorted lists

9.1 Sequential Search

In many cases, an array is used for storing a list of information. Having stored the information, it

may be required to find a given item in the list. For example, an array may be used to store a list

of the names of 50 people. It may then be required to find the position in the list at which a given

name (Indira, say) is stored.

We need to develop a technique for searching the elements of an array for a given specific

one. Since it is possible that the given item is not in the array, our technique must also be able

to determine this. The technique for searching for an item is the same regardless of the type

of elements in the array. However, the implementation of the technique may be different for

different types of elements.

We will use an integer array to illustrate the technique called sequential search. Consider the array

num of seven integers:

243

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!