2

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"

BenMorel
36.9k52 gold badges208 silver badges339 bronze badges
asked Dec 14, 2013 at 16:43
3
  • stackoverflow.com/questions/15995458/… Commented Dec 14, 2013 at 16:51
  • 2
    ouch. 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. Commented 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. Commented Dec 14, 2013 at 17:08

7 Answers 7

16

This figure should be helpful :

enter image description here

Then to answer your question, what would you do on paper ?

  1. Create and initialize the min value at tenIntArray[0]
  2. 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])
  3. Loop through the elements of your array
  4. 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
  5. You're done

Writing the algorithm should be straightforward now.

answered Dec 14, 2013 at 16:53
0
4

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;
 }
}
TruVortex_07
2801 gold badge3 silver badges16 bronze badges
answered Dec 14, 2013 at 18:57
0
2

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();
answered Apr 11, 2017 at 7:36
2
  • May I know the time complexity of this approach? Commented Aug 31, 2017 at 16:13
  • Will be O(n) I guess Commented Nov 6, 2017 at 9:59
1

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

answered May 11, 2015 at 9:37
0
 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);
answered Jul 30, 2015 at 10:22
0

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; 
}
answered Aug 31, 2016 at 16:15
-2

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");
answered Dec 14, 2013 at 17:14
4
  • Yes I did. can you tell me about the fault? Commented Dec 15, 2013 at 5:18
  • 1
    It'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 the index 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 for small). Commented Dec 15, 2013 at 9:21
  • ah I got that.thanks.if I start with int index = -1; , is that correct? Commented Dec 16, 2013 at 5:24
  • You should increase i outside of the if statement, because even if the current index is not the smallest, it should increase the i for keeping track of the current index. Commented Dec 3, 2014 at 9:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.