Arrays

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

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

View Full Document Right Arrow Icon
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; if (big_array[loc] == value) found = 1; else loc = loc + 1; if (found) return loc; else return –1; } 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
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: if (big_array[loc] >= value) found = 1; else loc = loc + 1; } found = 0; 2
Background image of page 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. Binary search
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.

Page1 / 9

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

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