Solved

Determine min array element no?

Posted on 1998-07-23
3
222 Views
Last Modified: 2010-03-30
I want to know is there any method provided in Java to help us find the array element no. which has the smallest value among all. Eg. A[0] = 1, A[1] = 3, A[2] = 0. Therefore, the smallest value is '0', and the element no is '2'. Thus, can I get the element no '2' by using any easy method.

Thanks!
0
Comment
Question by:yuho
3 Comments
 

Accepted Solution

by:
tomd012698 earned 50 total points
ID: 1227842
 There is no pre-existing method that will accomplish what you want, but you could write one yourself.  Try using this code.

int findSmallestIndex( int arr[], int length )
{
  int nSmallVal = Integer.MAX_VALUE;
  int nSmallIndex = 0;

  for ( int n = 0; n < length; n++ )
  {
    if ( arr[ n ] < nSmallVal )
    {
       nSmallVal = arr[ n ];
       nSmallIndex = n;
    }
  }
  return nSmallIndex;
}

  When multiple entries have the same value, the first one encountered will always win.  This is a brute force approach to this problem, but will be acceptable as long as you don't need to store a very large number of elements, and will not be calling this function a large number of times.  In those cases, I would suggest presorting the array, and simply looking at the value of the first element (if this fits into your needs).
0
 
LVL 1

Expert Comment

by:dryang
ID: 1227843
hi yuho,

you can dig around for books with the kind of sorting algorithms you need, even to sorting objects. Try this one:

Java Algorithms by Scott Robert Ladd,Mc Graw Hill 1997
0
 

Author Comment

by:yuho
ID: 1227844
Thanks!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now