1

For example I have an array like below:

int[] arrayOne = new int[10]{3,1,29,5,2,4,7,10,38,42};

And below are my method to pick the minimum number from the array

       int pickMinNumber (int[] intArray)
        {
          int result = intArray[0];
          for (int i = 0; i < arrayOne.Length; i++)
           {
                if(intArray[i] < result)
                   result = intArray[i];
           }
          return result;
        }

Are there any quicker way to pick the minimum number?

User2012384
  • 4,769
  • 16
  • 70
  • 106
  • 1
    In terms of algorithm, no. – tia Jun 09 '13 at 02:22
  • See also http://stackoverflow.com/questions/424800/what-is-the-best-way-to-get-the-minimum-or-maximum-value-from-an-array-of-number – maxwellb Jun 09 '13 at 02:24
  • _"pick the minimum number from the array"_ - as @dasblinkenlight hints to, your current implementation can be reduced to `return intArray[0]`, since the currently provided input seems to be almost sorted ascendingly. It just depends on the data structure and order you use. If it really is an ordered list (take a look at insertion sort), you can take the first item to find the lowest value, which is the fastest it gets: O(1). – CodeCaster Jun 09 '13 at 02:24
  • You have a couple off-by-one errors in your for loop. You don't need to compare the first element again and you are not checking the last element. Use `for (int i = 1; i < arrayOne.Length; i++)` to fix these issues. – Mike Zboray Jun 09 '13 at 02:27
  • @CodeCaster The input is not sorted. – Mike Zboray Jun 09 '13 at 02:28
  • @mike see edit, but the point is not whether the current input is sorted, but that if you want the selection to be fast, the insertion will be slow. – CodeCaster Jun 09 '13 at 02:29

4 Answers4

6

Are there any quicker way to pick the minimum number?

No, this is as quick as it gets. You cannot find the minimum any faster, unless array is sorted, or its elements follow any other special placement strategy: you must examine all elements in order to find the minimum, so any algorithm would do essentially the same thing as your code.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

Use the Min() function in LINQ

var min = arrayOne.Min();
arunlalam
  • 1,838
  • 2
  • 15
  • 23
  • This is the quickest to write, for sure. – maxwellb Jun 09 '13 at 02:23
  • 1
    downvoted, Because this is not necessarily going to be faster than what op suggested, in fact, if I understand LINQ it is only a syntactical shortcut for actual implementation in IL which will probably use same or similar algorithm as op suggested. – Charles Bretana Jun 09 '13 at 02:24
  • 2
    Faster in which domain? There's time-to-code, time-to-maintain, time-to-test, and time-to-run. Which do we mean? Not specified. I love answers that don't assume. – maxwellb Jun 09 '13 at 02:26
  • 1
    @maxwellb: time-to-maintain – User2012384 Jun 09 '13 at 02:31
  • Feel free to include your specific goals in the question. You might phrase the question "Are there any C#-specific libraries or methods that make finding the minimum value faster to write than having to loop through the entire array?" – maxwellb Jun 09 '13 at 02:41
2

This algorithm also called a linear search uses only one loop that is it is O(n) algorithm , it cant get any faster .

abkds
  • 1,764
  • 7
  • 27
  • 43
1

Let say I write 5 numbers on 5 piece of paper and face it down. I show you 4 numbers. Then, I ask you what is the smallest number among the 5 number. Can you answer this question without seeing the fifth number? If the answer is no, then of course, you cannot get any faster.

invisal
  • 11,075
  • 4
  • 33
  • 54