Algorithm Implementation for Finding Maximum and Minimum Values in Java Without Using Arrays

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: Java Algorithm | Extreme Value Finding | Loop Control

Abstract: This article provides a comprehensive exploration of algorithm implementations in Java for finding the maximum and minimum values in a set of numbers without utilizing array structures. By analyzing common issues encountered by developers in practical programming, particularly in initialization logic and boundary condition handling, the article offers complete code examples with step-by-step explanations. Key discussions focus on proper variable initialization, handling special cases for the first input value, and updating extreme values through loop comparisons. This implementation avoids array usage, reducing memory overhead, and is suitable for scenarios requiring dynamic input processing. Through comparative analysis of erroneous and correct code, the article delves into critical details of algorithmic logic, helping readers understand core concepts of loop control and conditional judgment.

Problem Background and Algorithm Requirements

In Java programming, processing a set of numbers to find their maximum and minimum values is a common task. Traditional solutions typically use arrays to store all input values and then traverse the array for comparisons. However, in certain scenarios, using arrays may introduce unnecessary memory overhead, especially when dealing with large input quantities. Therefore, developing an algorithm implementation that does not rely on arrays has practical significance.

Common Error Analysis

Many developers encounter errors in initialization logic when attempting to implement this algorithm. For instance, in the provided Q&A data, the user initially set the smallest variable to 0, which could lead to incorrect results, particularly when all input values are positive. The correct approach is to assign the first input value to both the smallest and large variables as the initial comparison baseline.

Complete Algorithm Implementation

Below is the complete Java code implementation based on the best answer:

public static void main(String[] args) {
    int smallest = 0;
    int large = 0;
    int num;
    System.out.println("enter the number");
    Scanner input = new Scanner(System.in);
    int n = input.nextInt();
    num = input.nextInt();
    smallest = num;
    for (int i = 2; i < n; i++) {
        num = input.nextInt();
        if (num > large) {
            large = num;
        }
        if (num < smallest) {
            smallest = num;
        }
    }
    System.out.println("the largest is:" + large);
    System.out.println("Smallest no is : " + smallest);
}

Code Logic Detailed Explanation

The core of this algorithm lies in the comparison logic within the loop. First, the program reads the number of values n input by the user, then reads the first value and assigns it to both the smallest and large variables. In subsequent loop iterations, each new value read is compared with the current maximum and minimum values, updating these variables when necessary.

Key Improvement Points

Compared to the initial erroneous code, the main improvements in this implementation include:

Algorithm Complexity Analysis

This algorithm has a time complexity of O(n), where n is the number of input values. Since no additional array storage space is required, the space complexity is O(1), using only a fixed number of variables. This makes the algorithm more efficient in terms of memory usage compared to array-based implementations.

Applicable Scenarios and Limitations

This array-free extreme value finding algorithm is particularly suitable for:

However, the limitation of this method is that it cannot retain historical records of all input data, providing only the final extreme value results.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.