# Thread: Algorithm for finding max and min in an array.

1. Member
Join Date
Oct 2010
Posts
56
Rep Power
0

## Algorithm for finding max and min in an array.

Hi guys, hope you are well. I am trying to create an algorithm which finds the maximum and minimum in an array A, with length n.

I have an algorithm here, but by accident I forgot it was from an array, instead I did from a list of command line arguments. Here is my algorithm:

Java Code:
```int maxNumber
int minNumber

for (i from 0 upto length of elements)
{ int aGivenValue
{ if aGivenValue>maxNumber)
then maxNumber = aGivenValue
}
}

for (i from 0 upto length of elements)
{ int anotherGivenValue
{ if anotherGivenValue < minNumber)
then minNumber = anotherGivenValue
}
return min and max value
}
}```
I'd like to tweek it so it uses arrays. Any suggestions I'm really stuck :( . I've also created this program and it works but it doesn't use an array, so how would I make it use arrays.

Kind regards

Shyam

2. ## Re: Algorithm for finding max and min in an array.

What have you tried in real code? That pseudocode seems pretty good. You can find the length of some array by using arrayName.length.

3. Member
Join Date
Oct 2010
Posts
56
Rep Power
0

## Re: Algorithm for finding max and min in an array.

I've sorted it. I'm stuck on this other one now. I'm doing greatest common divisor. I've done some pseudocode, but I now need to create another algorithm which uses Euclidean algorithm to find the GCD of two values. Here is my first algorithm:

Java Code:
```int u
int v
int multiple1 = u
int multiple2 = v

while(multiple1 is not equal to multiple2)
if multiple1 > multiple2
then multiple1-multiple2
else
then multiple2-multiple1

s.o.p "GCD of " + u + " and " + v + "is" + multiple1```
How can i write another algorithm using Euclidean to find gcd. I'm stuck on this part.. i don't understand how i would do it.
Last edited by Shyamz1; 10-04-2011 at 09:55 PM.

4. ## Re: Algorithm for finding max and min in an array.

How would you do Euclidean gcd of 135 and 45 by hand?

5. ## Re: Algorithm for finding max and min in an array.

Also, in case you need some help on how euclidean GCD works, here is a bit of a tip:

Given two integers, a and b, if r is the remainder of a divided by b, then the common divisor of a and b is also a divisor of r. It is straightforward enough to develop this recursively.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•