Chapter 12

Searching and Sorting Arrays

This chapter describes the most common and simplest algorithms used for searching a value in an array and for sorting the elements of an array in ascending or descending order.

Searching Arrays

To check if a value is stored into an array, we are going to describe the linear and binary search algorithms.

Linear Search

The linear search algorithm (also called sequential search) is the simplest algorithm to search for a value in a nonsorted array. The searched value is compared against the value of each element until a match is found.

In an array of n elements, the maximum number of searches is n. This may occur if the searched value is not found or is equal to the last element.

In the following program, ...

Get C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.