Google Search

Monday, March 28, 2011

cs2259 SMALLEST ELEMENT IN AN ARRAY


SMALLEST ELEMENT IN AN ARRAY


AIM:
            To find the smallest element in an array.

ALGORITHM:
  1. Place all the elements of an array in the consecutive memory locations.
  2. Fetch the first element from the memory location and load it in the accumulator.
  3. Initialize a counter (register) with the total number of elements in an array.
  4. Decrement the counter by 1.
  5. Increment the memory pointer to point to the next element.
  6. Compare the accumulator content with the memory content (next                             element).
  7. If the accumulator content is smaller, then move the memory content                            (largest element) to the accumulator. Else continue.
  8. Decrement the counter by 1.
  9. Repeat steps 5 to 8  until the counter reaches zero
  10. Store the result (accumulator content) in the specified memory location.


RESULT:
            Thus the smallest number in the given array is found out.

0 comments:

Post a Comment