Results 1 to 5 of 5
 10042011, 12:16 PM #1Member
 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 } }
Kind regards
Shyam
 10042011, 03:21 PM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
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.
 10042011, 08:52 PM #3Member
 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 multiple1multiple2 else then multiple2multiple1 s.o.p "GCD of " + u + " and " + v + "is" + multiple1
Last edited by Shyamz1; 10042011 at 08:55 PM.
 10042011, 08:59 PM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
Re: Algorithm for finding max and min in an array.
How would you do Euclidean gcd of 135 and 45 by hand?
 10042011, 10:45 PM #5
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 9
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.
Similar Threads

Help with algorithm for sorting an array of Strings
By eIO in forum New To JavaReplies: 5Last Post: 07112011, 01:40 PM 
Finding the Mode in An Array
By carlodelmundo in forum New To JavaReplies: 23Last Post: 10312010, 01:44 PM 
Finding a the max value of the array using a for loop
By soccer_kid_6 in forum New To JavaReplies: 1Last Post: 04112010, 11:25 PM 
Difficulty in finding the right algorithm
By SolidCobra in forum New To JavaReplies: 3Last Post: 10052008, 11:55 PM 
Finding a connection between cities using leastcost search algorithm
By Java Tip in forum java.langReplies: 0Last Post: 04122008, 08:38 PM
Bookmarks