- Initialize a variable M to Integer.MIN_VALUE - Compare each element in the array to M - If the element is smaller than M, set M equal to that element's value.
When you get to the end of the array, M is the minimum value in the array. If you want the index of the minimum value, then just keep a second variable to hold the index, and set it each time you set M.
You could use the first element, but then you have to check for the special case of zero-length arrays. [ February 23, 2006: Message edited by: Ernest Friedman-Hill ]
Vishnu Prakash
Ranch Hand
Joined: Nov 15, 2004
Posts: 1026
posted
0
Initializing it to the first element of the array would be fine.
don't you still need to check for 0 length arrays? i don't think returning Integer.MAX_VALUE is the correct answer if i have NO data
Never ascribe to malice that which can be adequately explained by stupidity.
Ray Dutil
Greenhorn
Joined: Jan 11, 2006
Posts: 3
posted
0
Wouldn't you be better off using some type of collection like an ArrayList or something? Or do you have to use a simple Array?
Life can only be understood backwards; but it must be lived forwards. - Kierkegaard
Robert Hill
Ranch Hand
Joined: Feb 24, 2006
Posts: 94
posted
0
Or you can write a simple bubblesort and then just get the lowest value in element 0. It might actually be faster this way, even with a slower sort algorithm like bubblesort.
Using ArrayList defeats the purpose of learning how to program. Which is better?
A. Someone who learns Java and a good chunk of the API, might even get one or two of those certificates of dubious value.
or
B. Someone who learns how to program, understands the underlying contructs of library classes and computers themselves and can easily move to other languages and even write their own libraries and therefore can leverage existing libraries better then A.