Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Smallest Element in an Array
3.
Approach 1(Iterative Method)
3.1.
Implementation
3.2.
Output
4.
Approach 2(Top-Down Recursion)
4.1.
Implementation
4.2.
Output
5.
Approach 3(Bottom-up Recursion)
5.1.
Implementation
5.2.
Output
6.
Frequently Asked Questions
7.
Conclusion
Last Updated: Mar 27, 2024

Smallest Element in an Array

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In today's environment, computer programming is essential. Without it, you would not be reading this article. In fact, you wouldn't be able to use your phone, computer, or smart TV if it weren't for computer programming.

Learning programming principles will help you stand out from the crowd and gain a competitive advantage in today's technology-driven environment.

This article will discuss printing the smallest element in a given array using various Java language methods to enhance your skills in the Java programming language.

Smallest Element in an Array

An array is a collection of elements of the same kind stored in contiguous memory locations. It may be accessed separately using a unique identifier's index.

A number array, such as an integer array, float array, double array, or long array, can hold numbers of various sizes. In an array, we can discover the smallest number of these.

To follow these examples, you need to be familiar with the following Java programming concepts:

  1. Loops in Java
  2. Methods in Java
  3. Concept of Recursion

We'll look at a few different approaches for finding the smallest element in an array of elements.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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);  
    }  
}  

Output

The Smallest element in the given array: 17

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:

  1. Creation of a Recursive Function: Construct a recursive function with the name getmin (int arr[], int num)
  2. Deciding a base condition: If (num==1) is true, return arr[0]
  3. 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));
}
}

Output

17

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)); 
  }
}

Output

17

Time Complexity: O(n)

Space Complexity: O(1)

Must Read What are Loops in Java, and Duck Number in Java.

Frequently Asked Questions

  1. 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.
     
  2. 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.
     
  3. 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 javaInheritance 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!!

 

Previous article
Largest Element in an Array
Next article
Program to find Sum of Natural Numbers
Live masterclass