Skip to content

Instantly share code, notes, and snippets.

@NusZzz NusZzz/gist:8116808
Created Dec 24, 2013

Embed
What would you like to do?
Algorithm for finding maximum value in array java by using Brute Force
public class FindMax {
public static void main(String[] args) {
int[] array = new int[]{3,7,4,9,5,6,8,1,20,2};
int currentMax = array[0];
for(int i=1;i<=array.length-1;i++){
if(currentMax < array[i]){
currentMax = array[i];
}
}
System.out.println("Maximum value is "+ currentMax);
}
}
@countersoda

This comment has been minimized.

Copy link

countersoda commented Nov 3, 2018

I might be mistaken but if the max number is in the first position, the algorithm is not able to catch that.

@DSamuylov

This comment has been minimized.

Copy link

DSamuylov commented Feb 7, 2019

I might be mistaken but if the max number is in the first position, the algorithm is not able to catch that.

You are mistaken, because your initial assumption is that a max number is at the first position. So if there is no elements in the array larger than the first element, you will return the first number at the first position.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.