views:

533

answers:

9

title says it all! is there a very simple algorithm to figure out which of 4 numbers is the greatest?

+2  A: 

Put the numbers into an array, sort the array, then select the one whose index is array length -1.

Or you could put the numbers into an array, sort the array, reverse the array, and then select index 0.

If you need to write your own sorting algorithm, the simplest one to implement is likely to be the bubble sort.

TheTXI
@TheTXI Are you joking? Why would anyone need to write his/her own sorting algorithm for something like this and why would you recommend bubble sort?
Sinan Ünür
Sinan Unur: Because this sounds like a homework problem.
TheTXI
Overslacked: Bingo.
TheTXI
@TheTXI Hahaha I like this answer. Bubble sort rulez cuz it has the word bubble in it! It wont get him an A but its a creative way to do it the most non-efficient way.
Chap
Bubble sort is pretty damn fast when n=4.
Adam Jaskiewicz
Insertion sort is a touch simpler to write, and usually has slightly better performance. There is almost always no reason to write a bubble sort.
David Thornley
David: You'd be right. It's been so long since I wrote one that I forgot about them and now my mind is stuck thinking that bubble sort is the most basic because it's the easiest one I remember :P
TheTXI
Except he's not asking to SORT the array, he's asking which element is the greatest. Bubble/insertion/any-sort is pointless in this exercise.
Chris Kaminski
Actually bubblesort is somewhat well suited to this problem, as you will be guaranteed that the last element is the maximum value (assuming you are bubbling larger numbers up) after a single pass of bubblesort. In other words, you only need to do one pass of bubblesort (O(n)) and then check the last element.Much faster than using any other sorting algorithm, but slower than just checking each value and storing the current max.
Niki Yoshiuchi
+13  A: 
  var lst = new List<int>() { 1, 7, 3, 4 };
  var max = lst.Max();

I got no VB, but you get the idea.

JP Alioto
Forgot about the Max()
TheTXI
Probably wont get you an A, but that's the practical way. I think they would want you to come up with the algorithm not LINQ.
Chap
VB is almost the same: http://pastebin.com/fb2c69beBeing cautious - been a while since I've done VB. Also, remember that you need LINQ - so add your "using System.Linq", and your reference to "System.Core" (You don't need "System.Data.Linq".)
Lucas Jones
+10  A: 

If they are in an array, something like this should work:

VB:

Dim ar As Integer() = {3, 6, 9, 12}
Dim largest As Integer = ar(0)
For i As Integer = 1 To ar.Length - 1
    If ar(i) > largest Then
        largest = ar(i)
    End If
Next

C#:

int[] ar = {3, 6, 9, 12};
int largest = ar[0];
for(int i = 1; i < ar.Length; i++) {
    if(ar[i] > largest) {
        largest = ar[i];
    }
}
Nate Bross
This is generalizable and requires only one pass over the data.
Sinan Ünür
This will also work with any version of Visual Basic (even pre .NET); and the algorithm will work for any language.
Nate Bross
+2  A: 

There are plenty of ways you could do this. A really naive approach would be:

#Pseudocode
If number1 > number2 and number1 > number3 and number1 > number4: return number1
Else if number2 > number3 and number2 > number4: return number2
Else if number3 > number4: return number3
Else: return number4

It's more practical to use arrays but if you're starting that could be more complicated than simple if blocks.

fmartin
+2  A: 

If they're in an array - and doing it explicitly rather than using sort:

int max = int.MinValue;   // i.e. the "largest" negative number
int largest = -1;
for (int index = 0; index < array.Length; index++)
{
    if (array[index] > max)
    {
        max = array[index];
        largest = index;
    }
}

The greatest value will be max and it's index in largest.

Nate's answer is more efficient as it uses the first element of the array as the initial value. So the first three lines of my solution would become:

int max = array[0];
int largest = 0;
for (int index = 0; index < array.Length; index++)
ChrisF
+2  A: 

If you're using a language that supports some sort of max function or array sorting definitely use those features. Or choose any of the other sane answers in this thread. However, just for fun:

maximum = (var1 > var2 ? var1 : var2) > (var3 > var 4 ? var3 : var 4) ? (var1 > var2 ? var1 : var2) : (var3 > var 4 ? var3 : var 4);

jeremy
I did this on an exam once (though it was only for three numbers).
Adam Jaskiewicz
My eyes are hurting just glancing at that code!! :'(
Richard Ev
A: 

My first question would be why? Second would be, if it's only four numbers then it really doesn't matter. Whatever takes your fancy. I personally would go with the fewest lines of code. Which would be to use the built in array.Sort method, then take the last item.
I would also consider using LINQ, just because you can.
Or Math.Max in a nasty nested way so Math.Max(Number1,Math.Max(Number2,Math.Max(Number3,Number4))))
If there could be hundreds of numbers, then I would try and pick a better algorithm. Probably the one suggested by @ChrisF, although it would depend on where the numbers are coming from, EG a database could find the max much easier, or if the numbers are being read from somewhere sequentially then you could store the max as you read the numbers.

pipTheGeek
What does it matter, why?
BryanH
+3  A: 

With VB.Net you could the following and it will work for any number of numbers

Public Function Max(ParamArray items As Integer()) As Integer
  if items.Length = 0 Then
    throw New ArgumentException("need at least 1 number")
  End IF
  return items.Max()
End Function

Then you can now do

Max(1,2,3,4)
JaredPar
A: 

In Java, if a is an int[4]:

Math.max(Math.max(a[0], a[1]), Math.max(a[2], a[3]))