How can I find the smallest value in a int array without changing the array order?
code snippet:
int[] tenIntArray = new int [10];
int i, userIn;
Scanner KyBdIn = new Scanner(System.in);
System.out.println("Please enter 10 integer numbers ");
for(i = 0; i < tenIntArray.length; i++){
System.out.println("Please enter integer " + i);
userIn = KyBdIn.nextInt();
tenIntArray[i] = userIn;
}
I am not sure how I can find the smallest array value in the tenIntArray and display the position
For example the array holds - [50, 8, 2, 3, 1, 9, 8, 7 ,54, 10]
The output should say "The smallest value is 1 at position 5 in array
"
-
stackoverflow.com/questions/15995458/…Guy Gavriely– Guy Gavriely2013年12月14日 16:51:18 +00:00Commented Dec 14, 2013 at 16:51
-
2ouch. Index counting starts at 0, not 1. As a condition you should use i < tenIntArray.length and not !=, just to be safe when you start using doubles or floats.extraneon– extraneon2013年12月14日 16:51:51 +00:00Commented Dec 14, 2013 at 16:51
-
@extraneon It's not "just to be safe", it's the more logical, generally accepted way of doing things.Bernhard Barker– Bernhard Barker2013年12月14日 17:08:48 +00:00Commented Dec 14, 2013 at 17:08
7 Answers 7
This figure should be helpful :
enter image description here
Then to answer your question, what would you do on paper ?
- Create and initialize the min value at
tenIntArray[0]
- Create a variable to hold the index of the min value in the array and initialize it to 0 (because we said in 1. to initialize the min at
tenIntArray[0]
) - Loop through the elements of your array
- If you find an element inferior than the current min, update the minimum value with this element and update the index with the corresponding index of this element
- You're done
Writing the algorithm should be straightforward now.
Try this:
//Let arr be your array of integers
if (arr.length == 0)
return;
int small = arr[0];
int index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < small) {
small = arr[i];
index = i;
}
}
Using Java 8 Streams you can create a Binary operator which compares two integers and returns smallest among them.
Let arr is your array
int[] arr = new int[]{54,234,1,45,14,54};
int small = Arrays.stream(arr).reduce((x, y) -> x < y ? x : y).getAsInt();
-
May I know the time complexity of this approach?Srikanth R– Srikanth R2017年08月31日 16:13:11 +00:00Commented Aug 31, 2017 at 16:13
-
The method I am proposing will find both min
and max
.
public static void main(String[] args) {
findMinMax(new int[] {10,40,50,20,69,37});
}
public static void findMinMax(int[] array) {
if (array == null || array.length < 1)
return;
int min = array[0];
int max = array[0];
for (int i = 1; i <= array.length - 1; i++) {
if (max < array[i]) {
max = array[i];
}
if (min > array[i]) {
min = array[i];
}
}
System.out.println("min: " + min + "\nmax: " + max);
}
Obviously this is not going to one of the most optimized solution but it will work for you. It uses simple comparison to track min
and max
values. Output is:
min: 10
max: 69
int[] input = {12,9,33,14,5,4};
int max = 0;
int index = 0;
int indexOne = 0;
int min = input[0];
for(int i = 0;i<input.length;i++)
{
if(max<input[i])
{
max = input[i];
indexOne = i;
}
if(min>input[i])
{
min = input[i];
index = i;
}
}
System.out.println(max);
System.out.println(indexOne);
System.out.println(min);
System.out.println(index);
Here is the function
public int getIndexOfMin(ArrayList<Integer> arr){
int minVal = arr.get(0); // take first as minVal
int indexOfMin = -1; //returns -1 if all elements are equal
for (int i = 0; i < arr.size(); i++) {
//if current is less then minVal
if(arr.get(i) < minVal ){
minVal = arr.get(i); // put it in minVal
indexOfMin = i; // put index of current min
}
}
return indexOfMin;
}
the first index of a array is zero. not one.
for(i = 0; i < tenIntArray.length; i++)
so correct this. the code that you asked is :
int small = Integer.MAX_VALUE;
int i = 0;
int index = 0;
for(int j : tenIntArray){
if(j < small){
small = j;
i++;
index = i;
}
}
System.out.print("The smallest value is"+small+"at position"+ index +"in array");
-
Yes I did. can you tell me about the fault?user3101937– user31019372013年12月15日 05:18:44 +00:00Commented Dec 15, 2013 at 5:18
-
1It's find the smallest but not the correct index. For example with the input
int [] tenIntArray = {1,-8,2,2,0,-15};
, you will increment theindex
variable 3 times. But that won't give you the correct index of the minimum value in the array. You have to keep track of the current index, and update another variable when you have found a smallest value (like you did forsmall
).Alexis C.– Alexis C.2013年12月15日 09:21:52 +00:00Commented Dec 15, 2013 at 9:21 -
ah I got that.thanks.if I start with
int index = -1;
, is that correct?user3101937– user31019372013年12月16日 05:24:17 +00:00Commented Dec 16, 2013 at 5:24 -
You should increase
i
outside of theif
statement, because even if the current index is not the smallest, it should increase thei
for keeping track of the current index.Jens Kooij– Jens Kooij2014年12月03日 09:41:47 +00:00Commented Dec 3, 2014 at 9:41