How to find the smallest number in an array Java


JavaViews 2614

In this tutorial, we will see the different approaches on how to find the smallest number in an array in Java.

How to find the smallest number in array Java

Sort and swap technique

In this method, we actually compare the 1st element with the 2nd element and sort the elements in ascending order using a third variable. In this way, after sorting, the first array element will be the smallest number in the array.

How to find the smallest number in an array Java

Algorithm

  1. Initialize an array variable arrValues with array values.
  2. Next, initialize an int variable small and assign it as arrValues[0] – This variable holds the first element by default.
  3. Initialize outer for loop starting from 0 until array length.
  4. Initialize inner for loop starting from i+1 until array length.
  5. Compare arrValues[i] > arrayValues[j]
  6. If true, swap the values using the small variable.
  7. If false, move to the next iteration within the inner for loop
  8. Continue steps 3 to 7 until it reaches the end of iteration in outer for loop.
  9. Finally arrValues variable contains a sorted array list in ascending order.
  10. Hence arrValues[0] contains the smallest element.

Java program: How to find the smallest number in an array

Below is an example that shows how to find the smallest number in an array Java using the sort and swap technique using a third variable.

public class SmallNumDemo1 {

  public static void main(String[] args) {
    int[] arrValues = {23,56,12,85,68};
    int small = arrValues[0];
    
    for(int i=0;i<arrValues.length;i++) {
      for(int j=i+1;j<arrValues.length;j++) {
        if(arrValues[i] > arrValues[j]) {
          small = arrValues[i];
          arrValues[i] = arrValues[j];
          arrValues[j] = small;
        }
      }
    }

    System.out.println("The smallest element is: " + arrValues[0]);
  }

}
The smallest element is: 12

Comparison technique

This is another simple method where we can find the smallest element. Here, we initialize a temp variable as the first element. We then compare this element with all other elements in the array. Whenever the array element value is less than the temp variable, we reassign the temp variable with the array element. This means, finally the temp variable will hold the smallest value.

Algorithm

  1. Initialize array variable arrValues an array of integer values.
  2. Declare and initialize an int variable temp with arrValues[0]. This variable holds the first element by default
  3. Initialize a for loop starting from 0 until array length
  4. Compare arrValues[i] < temp – This means, comparing every variable in the array with the first element
  5. If true, reassign the temp variable with the current array value.
  6. Repeat this, until the end of the loop.
  7. Finally, the temp variable contains the smallest element.

Java Program: How to find the smallest number in an array

Below is an example that shows how to find the smallest number in an array in Java using the comparison technique using a temp variable.

public class SmallNumDemo2 {

  public static void main(String[] args) {
    int[] arrValues = {45,10,60,5,23};
    int temp = arrValues[0];
    
    for(int i=0;i<arrValues.length;i++) {
      if(arrValues[i] < temp) {
        temp = arrValues[i];
      }
    }
    
    System.out.println("The smallest element is " + temp);

  }

}
The smallest element is 5

Using Arrays class

The Arrays class has sort() method that automatically sorts all the elements in the array in ascending order. Hence we can retrieve the smallest element directly by using array index 0 of the array variable. This means arrNum[0] returns the smallest element in the array.

You might be interested in How to return an array in Java

Below is an example that shows how to find the smallest number in an array in Java using the Arrays class.

import java.util.Arrays;

public class SmallNumDemo3 {

  public static void main(String[] args) {
    int[] arrNum = {56,12,45,23,89,33};
    
    Arrays.sort(arrNum);
    System.out.println("The smallest element is: " + arrNum[0]);
    
  }

}
The smallest element is: 12

Using Collections class

The Collections class also has sort() method that sorts the elements in the array in ascending order. First, we initialize an array variable. Then, convert it to Integer List using the asList method. Finally, we can sort this list using the Collection’s sort method. Hence the element at index 0 returns the smallest element.

Below is an example that shows how to find the smallest number in an array in Java using the Collections class.

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SmallNumDemo3 {

  public static void main(String[] args) {
    Integer[] arrNum = {56,12,45,23,89,33};
    List<Integer> arrList = Arrays.asList(arrNum);
    Collections.sort(arrList);
    
    System.out.println("The smallest element is: " + arrList.get(0));
    
  }

}
The smallest element is: 12

Reference

Translate »