Finding the second smallest integer in array

Majd Khoury

We are required in our assignment to find the second smallest integer in one array recursively. However, for the sake of understanding the subject more, I want to do it iteratively first (with the help of this website) and recursively on my own.

Unfortunately, doing it iteratively is quite confusing. I understand that the solution is simple but i can't wrap my head around it.

Below is my code, so far:

public static void main(String[] args) 
{
    int[] elements  = {0 , 2 , 10 , 3, -3 }; 
    int smallest = 0; 
    int secondSmallest = 0; 

    for (int i = 0; i < elements.length; i++)
    {
        for (int j = 0; j < elements.length; j++)
        {
            if (elements[i] < smallest)
            {
                smallest = elements[i];

                if (elements[j] < secondSmallest)
                {
                    secondSmallest = elements[j];
                }
            }
        }

    }

    System.out.println("The smallest element is: " + smallest + "\n"+  "The second smallest element is: " + secondSmallest);
}

This works for a few numbers, but not all. The numbers change around because the inner if condition isn't as efficient as the outer if condition.

Array rearrangements are forbidden.

nesteant

Try this one. Second condition is used to catch an event when the smallest number is the first

    int[] elements = {-5, -4, 0, 2, 10, 3, -3};
    int smallest = Integer.MAX_VALUE;
    int secondSmallest = Integer.MAX_VALUE;
    for (int i = 0; i < elements.length; i++) {
        if(elements[i]==smallest){
          secondSmallest=smallest;
        } else if (elements[i] < smallest) {
            secondSmallest = smallest;
            smallest = elements[i];
        } else if (elements[i] < secondSmallest) {
            secondSmallest = elements[i];
        }

    }

UPD by @Axel

int[] elements = {-5, -4, 0, 2, 10, 3, -3};
int smallest = Integer.MAX_VALUE;
int secondSmallest = Integer.MAX_VALUE;
for (int i = 0; i < elements.length; i++) {
    if (elements[i] < smallest) {
        secondSmallest = smallest;
        smallest = elements[i];
    } else if (elements[i] < secondSmallest) {
        secondSmallest = elements[i];
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding Second Smallest Element in Array

From Dev

Finding the smallest and second smallest value in an array Java

From Dev

Incorrect output when finding second smallest integer

From Dev

finding smallest and second smallest value of an array in C++

From Dev

finding smallest and second smallest number

From Dev

Finding the smallest integer

From Dev

Finding the k smallest odd integer

From Dev

Finding the k smallest odd integer

From Dev

Finding the smallest element in multidimensional array

From Dev

Finding the second smallest number using loops in python

From Dev

c program for smallest and second smallest in array

From Dev

Finding a single integer in an array

From Dev

finding a repeating integer in an array

From Dev

Recursion for finding the smallest path of numbers in an array

From Dev

Finding smallest neighbour in a 2D array

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

Finding smallest value from array in java

From Dev

Finding smallest neighbour in a 2D array

From Dev

Java - Finding Largest and Smallest Numbers using an Array

From Dev

java binary search tree finding second smallest node

From Dev

java binary search tree finding second smallest node

From Dev

Finding First and Second in an Array of Values?

From Dev

Finding First and Second in an Array of Values?

From Dev

Second smallest number in array - incorrect output

From Dev

Finding length of a loop in an integer array

From Dev

Given an array of integers, find the second largest and second smallest within the array

From Dev

Finding the array with the smallest Nth value in a JavaScript 2D Array

From Dev

Finding elements of an array that match elements of a second array

From Dev

Negative numbers not considered when finding smallest number in an array

Related Related

  1. 1

    Finding Second Smallest Element in Array

  2. 2

    Finding the smallest and second smallest value in an array Java

  3. 3

    Incorrect output when finding second smallest integer

  4. 4

    finding smallest and second smallest value of an array in C++

  5. 5

    finding smallest and second smallest number

  6. 6

    Finding the smallest integer

  7. 7

    Finding the k smallest odd integer

  8. 8

    Finding the k smallest odd integer

  9. 9

    Finding the smallest element in multidimensional array

  10. 10

    Finding the second smallest number using loops in python

  11. 11

    c program for smallest and second smallest in array

  12. 12

    Finding a single integer in an array

  13. 13

    finding a repeating integer in an array

  14. 14

    Recursion for finding the smallest path of numbers in an array

  15. 15

    Finding smallest neighbour in a 2D array

  16. 16

    Java - Finding Largest and Smallest Numbers using an Array

  17. 17

    Finding smallest value from array in java

  18. 18

    Finding smallest neighbour in a 2D array

  19. 19

    Java - Finding Largest and Smallest Numbers using an Array

  20. 20

    java binary search tree finding second smallest node

  21. 21

    java binary search tree finding second smallest node

  22. 22

    Finding First and Second in an Array of Values?

  23. 23

    Finding First and Second in an Array of Values?

  24. 24

    Second smallest number in array - incorrect output

  25. 25

    Finding length of a loop in an integer array

  26. 26

    Given an array of integers, find the second largest and second smallest within the array

  27. 27

    Finding the array with the smallest Nth value in a JavaScript 2D Array

  28. 28

    Finding elements of an array that match elements of a second array

  29. 29

    Negative numbers not considered when finding smallest number in an array

HotTag

Archive