ABSTRACT

This chapter describes some of the most common algorithms used for searching a value in an array and for sorting the elements of an array in ascending or descending order. It discusses the linear and binary search algorithms. The linear search algorithm is the simplest algorithm to search for a value in a nonsorted array. The binary search algorithm is used to search for a value in a sorted array. There are several algorithms to sort an array. The chapter focuses on the most popular, the selection sort, the insertion sort, the bubble sort, and the quick sort algorithms. The sel_sort() function implements the selection sort algorithm to sort an array in ascending order. The bubble sort algorithm is based on sequential comparisons between adjacent array elements. The quick sort algorithm is a particularly popular algorithm and the best choice for a variety of sorting applications. The chapter explains the bsearch() and qsort() library functions.