{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

1 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; } 2 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;

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

View Full Document