notes-sortsearch

This preview shows pages 1–4. Sign up to view the full content.

Searching and Sorting Last Modified 7/31/08 Linear Search Binary Search Search Efficiency Selection Sort Bubble Sort Insertion Sort Sorting Efficiency Other Sorts Linear Search When we conduct a search on an array, the element we're looking for is the search key . Sometimes, we may simply be looking to see if a particular value is present and where it is. Often, though, we're looking for more: the search key may be part of a larger record of information. For our purposes today, we'll look at simple searches on 1D arrays. In the last lab, we conducted a search in the following way: Go through the array one element at a time. Compare the current element to the search key. If there is a match, report success and the location. If we check all elements and have not found the key, report failure. This method of searching is called the linear search or the sequential search . Linear Search Binary Search Search Efficiency Selection Sort Bubble Sort Insertion Sort Sorting Efficiency Other Sorts Binary Search The linear search works on any array. If we know something about the array, we can improve upon our sorting method. Namely, if we know our array is sorted , we can use a searching algorithm called the binary search . (We'll assume the array is sorted in ascending order.) At each iteration, we narrow our search field by half. Here's how it goes in general: Compare the search key to the middle element of the array. It could be the key. If so, report success and the location. Otherwise, eliminate half of the array based on how the key compares to the middle element. Consider this animation [PPT].

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Formalizing It We need to know what part of the array we're searching, so we introduce a few variables for the important array indices: low mid high A few important notes about these indices: Since they are array indices, they must be integers . Remember int arithmetic - if the middle falls between two integers, "the middle" is the lower of the two. Commonly confused: these variables represent the array indices , NOT the values stored there. array[mid] is not the same as mid ! Comparing mid and the key is senseless: one is a location and one is a value. We also need a stopping point: when the high and low indices cross. The Code Now let's build the code! We'll do this as a function and begin by defining our high, low, and mid as local variables: int Search(const double array[], int size, double key) { int high = size-1; // high index - one less than size int low = 0; // low index - 0 for any array int mid; // middle index } Next, let's add in the code for one iteration of the search: int Search(const double array[], int size, double key) { int high = size-1; // high index - one less than size int low = 0; // low index - 0 for any array int mid; // middle index mid = (high+low)/2; // find middle index
if(key == array[mid]) // compare to middle

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.