{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Arrays - Arrays and Searching A very common programming...

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

Arrays and Searching A very common programming process. Sequential Search : the simplest method. Begin search at one end of the array and scan down it until the desired value is found or the other end is reached. Algorithm: int find(int big_array[], int size, int value) { int found=0, loc = 0; while(!found && loc < size){ if (big_array[loc] == value) found = 1; else loc = loc + 1; if (found) return loc; else return –1; } 1

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

View Full Document
Sorted Arrays What happens if the array is sorted? we can terminate search as soon as a value which is greater than or equal to the target value is found. Algorithm: while (!found && loc < size){ if (big_array[loc] >= value) found = 1; else loc = loc + 1; } if (found && big_array[loc] != value) found = 0; 2
Binary Search Sequential search is easy to implement and efficient for short arrays, but it is a disaster for long arrays. (e.g. trying to find “Smith” in a telephone directory with sequential search) To find an item in a long list, there are far more efficient methods.

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.

{[ snackBarMessage ]}