简体   繁体   中英

Smallest array element in Java

I have an Integer array, value[] . It may be of any length.

I need to find the smallest number. I am working on a problem in interview street. I am getting an error as my Time Exceeded . I need to increase the speed of computing the smallest value, as iterating gets worse.

I am interested in using Java collections or anything else, so anyone give some suggestions to improve the code.

int mini=value[0];
for (int i = 1; i < noinps; i++) {
    if(mini>value[i]) {
        mini=value[i];
    }
}
System.out.println(mini);

There isn't a better algorithm for finding the minimum or maximum element in an array. You have to look at every element. Maybe you lose time somewhere else - for example reading the input.

The algorithm seems fine for finding the minimum value of an array. If nothing is known about the possible elements contained in the arraym, you need to inspect each element, so your performance is linear (directly proportional to the number on elments in the array).

If the elements are fully ordered ordered, you can just look up the first (or last, dependng or ordering) element (constant time performance)

If the elements are semi-ordered (eg heap structure), you could find it via a binary search-like technique (log(n) performance)

If the elements are unordered, but you know they cannot be less than a certain value, you can stop the search if you find the smallest possible value.

So back to the timeout problem: if the elements are unordered and there are no other restrictions on them, your algorithm is fine, so the timeout must come form some other part of the program (eg you are trying to read from the console, but the data is in a file)

if you use array to be your storage structure for elements then you don't have any choice than to loop through all the elements to find the min element. however your algorithm works fine with O(n) complexity as you are looping only once through the array.

Iterating gets worse if the array size is very very huge. in that case i recommend not to use Arrays as a storage area, if you have no choice other than an array. then try to inspect each and every element before you put the element in the array itself, that way you no need to iterate the array for finding the min element.

to use collection, this would be best approach

ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add(new Integer("23"));
arrayList.add(new Integer("1"));
arrayList.add(new Integer("134"));
arrayList.add(new Integer("22"));
arrayList.add(new Integer("0"));

/*
   To find maximum element of Java ArrayList use,
  static Object max(Collection c) method of Collections class.

   This method returns the maximum element of Java ArrayList according to
   its natural ordering.
*/

Object obj = Collections.max(arrayList);

System.out.println("Maximum Element of Java ArrayList is : " + obj);

Use 2 threads, each one takes the half of the array, finds the minimum in its half, and then writes the result to a variable. After that, compare the two results in the main thread.

Easy way to get the smallest number..

 import java.util.Arrays;
    public class demoClass {
        public static void main(String[] args) {

                  // consider the below array of data
                    int[] numbers = {12,22,32,43,53};

                   // first sort the array using sort keyword
                   Arrays.sort(numbers);

                 System.out.println("Smallest Number = "+numbers[0]);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM