Approach 1(Iterative Method)
This program aims to discover the smallest element in the array. This can be done by keeping a variable called small, which will initially retain the value of the first element. By comparing the value of small with the array's items, you may loop across the array. If any element's values are less than small, the element's value is stored in small.
Let's consider an example; we have an array of 5 elements as follows:

At the initial stage, small will keep the value 22.
-
1st Iteration: small will be compared with 17. Since small>17, small will now store 17.
-
2nd Iteration: small will be compared with 23. Since small <23, small will not be updated and will store 17 only.

-
3rd Iteration: small will be compared with 25. Since small<25, small will not be updated.
-
4th Iteration: small will be compared with 18. Again small< 18. So the final value of small will be 17.
Here is the program to find the smallest:
Implementation
public class CodingNinjas01 {
public static void main(String[] args) {
//Initialise array
int [] arr = new int [] {22, 17, 23, 25, 18};
//Initialise small with first element of array.
int small = arr[0];
//Loop through the array
for (int i = 0; i < arr.length; i++) {
//Compare elements of array with small
if(arr[i] <small)
small = arr[i];
}
System.out.println("The Smallest element in the given array: " + small);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
The Smallest element in the given array: 17

You can also try this code with Online Java Compiler
Run Code
Time Complexity: O(n)
Space Complexity: O(1)
Click here to read about Array in Javascript here.
Approach 2(Top-Down Recursion)
The Top-down Recursive approach mainly consists of three steps:
- Creation of a Recursive Function: Construct a recursive function with the name getmin (int arr[], int num)
- Deciding a base condition: If (num==1) is true, return arr[0]
-
Otherwise condition: Return min(arr[num-1], getmin(arr, num-1)) if base condition is not true.

Implementation
// Recursive Java program to find minimum
import java.util.*;
class CodingNinjas02 {
// Recursive function to return minimum element using recursion
public static int findSmall(int arr[], int num)
{
// if num== 1 means we have only one element
if(num == 1)
return arr[0];
return Math.min(arr[num-1], findSmall(arr, num-1));
}
// Driver code
public static void main(String args[])
{
int arr[] = {22, 17, 23, 25, 18};
int num = arr.length;
// Calling Recursive Function
System.out.println(findSmall(arr, num));
}
}

You can also try this code with Online Java Compiler
Run Code
Output
17

You can also try this code with Online Java Compiler
Run Code
Time Complexity: O(n)
Space Complexity: O(1)
You can easily run this code yourself with java online compiler.
Also check out Addition of Two Numbers in Java here.
Approach 3(Bottom-up Recursion)
We can also find the smallest element in the array using the Bottom-up approach as well by following the given steps:
- We will use the Syntax: findmin(int arr[], int num, int end) to call the Recursive Function.
- Initially, end = index of the last element of the given array, num = 0.
- Recursively reach the iteration where the second final element is read.
- Between the 2nd last and last array elements, find the smaller.
- And set the result to the preceding recursive iteration's minimum value.
- Find the smaller of the current array index element and the current min value in each of the remaining recursive call ups.
- Print the final min value from the last recursive callup.
Implementation
import java.util.*;
class CodingNinjas03
{
static int findSmall(int arr[], int num, int end)
{
int min;
if(num == end-1)
return (arr[num] < arr[num + 1]) ? arr[num] : arr[num + 1];
min = findSmall(arr, num + 1, end);
return (arr[num] < min) ? arr[num]: min;
}
public static void main(String args[])
{
int arr[] = {22, 17, 23, 25, 18};
int end = arr.length-1;
System.out.print(findSmall(arr, 0, end));
}
}

You can also try this code with Online Java Compiler
Run Code
Output
17

You can also try this code with Online Java Compiler
Run Code
Time Complexity: O(n)
Space Complexity: O(1)
Must Read What are Loops in Java, and Duck Number in Java.
Frequently Asked Questions
-
Why is the main method Public in Java?
We know that a method with a public access specifier can be accessed/invoked by anyone. Because the JVM must invoke the main method, it is public in Java. The JVM will not call main() if it is not public in Java.
-
Why is the main method Static and void in Java?
Because the main() method is static, JVM can call it without having to instantiate the class. This also saves memory that would have been wasted if the object had been declared only for the JVM to call the main() method.
Because the main method in Java does not return anything, its return type is void.
-
What is the meaning of “ import java.util.*;”?
It is a built-in package with a comparable set of classes, sub-packages, and interfaces. The * allows users to import a class from the existing package and use it as many times as they need in the programme.
Conclusion
This article has discussed the Java Program to find the smallest element of a given array using iterative and Recursive Methods.
We hope this article has helped you. You can also learn Object-Oriented Properties of Java such as Abstraction in java, Inheritance in Java.
Recommended problems -
Visit Coding Ninjas Studio, our practice platform, to practise top problems, take mock tests, read interview experiences, and do more technical stuff.
We wish you Good Luck! Keep coding and keep reading Ninja!!