title says it all! is there a very simple algorithm to figure out which of 4 numbers is the greatest?
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.
var lst = new List<int>() { 1, 7, 3, 4 };
var max = lst.Max();
I got no VB, but you get the idea.
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];
}
}
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.
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++)
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);
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.
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)