## Programming Example 10: Sorting and Searching## IntroductionThese three programs illustrate - A Linear Search.
- A Binary Search.
- A Bubble Sort.
As you will see from the code, a linear search is easy to write. All you need is one
loop where you look at each element of the array in turn to see if it contains the
value that you are looking for. However, a linear search is not very efficient. If an array
has The source code for the linear search is here. The executable for the linear search is here. It takes a bit more code to do a binary search, and the logic is a little more complex. However, a binary search is
very efficient when compared to a linear search. This is because you eliminate one half of the search range each time that you
access the array. On average, you will have to access an array of The source code for the binary search is here. The executable for the binary search is here. The easiest sorting algorithm for an array is the bubble sort. The bubble sort is quite inefficient, but the logic is straightforward and it is easy to write. When you look at the code for the bubble sort, note the nested for loops and the swapping of values when they are out of order. The source code for the bubble sort is here. The executable for the bubble sort is here. |