{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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;
Image of page 1

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

View Full Document Right Arrow Icon