notes-sortsearch - Searching and Sorting Last Modified...

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

View Full Document Right Arrow Icon
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].
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
if(key == array[mid]) // compare to middle
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 02/10/2011.

Page1 / 9

notes-sortsearch - Searching and Sorting Last Modified...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online