Java Program to find Smallest Number in an Array

In this article, you will learn how to find the smallest number in an Array in java. To understand this program, you should have the basic knowledge of an integer array and the looping concept.

Algorithm-1.

  1. Declare and Initialize an integer array with some elements.
  2. Sort the array in ascending order.
  3. For sorting, write two loops (one inside the other), the first loop start from 0 to array length – 1, and the second loop start from i+1 to arr length.
  4. Inside the second loop, swap the value between arr[i] and arr[j] with a temporary variable, if arr[i] > arr[j].
  5. After the completion of loops, the 0th index value of the array will be the smallest number.
  6. Finally, print the smallest number.

Based on the above algorithm, we will see two examples (Example#1 and Example#2).

Example #1. Find the smallest number in an array

In this example, we declare and initialize an integer array and then sort them in ascending order, and after that, we found the smallest number at the 0th index in the array. Let’s see the java code for it below:

package com.javacodepoint.array;

public class SmallestNumberInArray {

	public static void main(String[] args) {

		// Declare and Initialize an integer Array
		int arr[] = { 4, 1, 8, -5, 8, 0, 12, 1 };

		// Sorting the array element in ascending order
		for (int i = 0; i < arr.length - 1; i++) {
			for (int j = i + 1; j < arr.length; j++) {
				if (arr[i] > arr[j]) {
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}

		// Array 0th element will be the smallest no.
		int smallNumber = arr[0];

		// Printing the smallest number
		System.out.println("The Smallest Number in the Array: " + smallNumber);

	}

}

OUTPUT:

The Smallest Number in the Array: -5

Example #2. Find the smallest number in an array java using Scanner

In this example, we declare and initialize the integer array from the user-given value using Scanner.

package com.javacodepoint.array;

import java.util.Scanner;

public class SmallestNumberInArray2 {

	public static void main(String[] args) {

		// Creating scanner object for reading user inputs
		Scanner sc = new Scanner(System.in);

		// Read the size of the Array
		System.out.println("Enter the size of the Array: ");
		int n = sc.nextInt();

		// Declare the integer Array of given size
		int arr[] = new int[n];

		// Reading the Array values
		System.out.println("Enter " + n + " element(s) of the Array: ");
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}

		// Sorting the array element in ascending order
		for (int i = 0; i < arr.length - 1; i++) {
			for (int j = i + 1; j < arr.length; j++) {
				if (arr[i] > arr[j]) {
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}

		// Array 0th element will be the smallest no.
		int smallNumber = arr[0];

		// Printing the smallest number
		System.out.println("The Smallest Number in Given Array: " + smallNumber);

	}

}

OUTPUT:

Enter the size of the Array:
5
Enter 5 element(s) of the Array:
2
6
0
6
10
The Smallest Number in Given Array: 0

Algorithm-2.

  1. Declare and Initialize an integer array with elements given by the user.
  2. Declare a smallNumber variable and initialize it with the 0th index of the array element. eg: smallNumber =arr[0].
  3. Now iterate the array from 1 to array length.
  4. Inside the loop, assign the arr[i] value into smallNumber variable, if it is less.
  5. After the completion of loops, the smallNumber variable will become the smallest number.
  6. Finally, print the smallest number.

Based on the above algorithm 2, we will see Example#3 below:

Example #3. Find the smallest number in array java without sorting

In this example, we will find the smallest number in an array without using any sorting technique. Let’s see the java code below:

package com.javacodepoint.array;

import java.util.Scanner;

public class SmallestNumberInArray3 {

	public static void main(String[] args) {

		// Creating scanner object for reading user inputs
		Scanner sc = new Scanner(System.in);

		// Read the size of the Array
		System.out.println("Enter the size of the Array: ");
		int n = sc.nextInt();

		// Declare the integer Array of given size
		int arr[] = new int[n];

		// Reading the Array values
		System.out.println("Enter " + n + " element(s) of the Array: ");
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}

		// Assume 0th element will be the smallest no.
		int smallNumber = arr[0];

		// Iterate the array from index 1
		for (int i = 1; i < arr.length; i++) {
			if (arr[i] < smallNumber) {
				smallNumber = arr[i];
			}
		}

		// Printing the smallest number
		System.out.println("The Smallest Number in Given Array: " + smallNumber);

	}

}

OUTPUT:

Enter the size of the Array:
5
Enter 5 element(s) of the Array:
7
2
12
8
9
The Smallest Number in Given Array: 2

Algorithm-3.

  1. Declare and Initialize an integer array with elements given by the user.
  2. Convert the given array into a list collection. eg: Arrays.asList(arr);
  3. Sort the list in ascending order. eg: Collections.sort(list);
  4. Get the 0th index value from the list. eg: list.get(0);
  5. The 0th index value will be the smallest number in the given array.
  6. Finally, print the smallest number.

Based on the above algorithm 3, we will see the below Example #4:

Example #4. Find the small number using Collections java

In this example, we use the collections to find the smallest number in java. Let’s see the code below:

package com.javacodepoint.array;

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

public class SmallestNumberInArray4 {

	public static void main(String[] args) {

		// Creating scanner object for reading user inputs
		Scanner sc = new Scanner(System.in);

		// Read the size of the Array
		System.out.println("Enter the size of the Array: ");
		int n = sc.nextInt();

		// Declare the integer Array of given size
		Integer arr[] = new Integer[n];

		// Reading the Array values
		System.out.println("Enter " + n + " element(s) of the Array: ");
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}

		// Convert int array into list collection
		List<Integer> list = Arrays.asList(arr);

		// Sort the list in ascending order
		Collections.sort(list);

		int smallNumber = list.get(0);

		// Printing the smallest number
		System.out.println("The Smallest Number in Given Array: " + smallNumber);

	}

}

OUTPUT:

Enter the size of the Array:
6
Enter 6 element(s) of the Array:
1
-4
8
-9
10
15
The Smallest Number in Given Array: -9

See also: Java Program to find Largest Number in an Array.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *