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); | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
DSamuylov
commented
Feb 7, 2019
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
This comment has been minimized.
countersoda commentedNov 3, 2018
I might be mistaken but if the max number is in the first position, the algorithm is not able to catch that.