forked from heitor582/Algoritmos-e-Estruturas-de-Dados
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxMinArray.java
38 lines (33 loc) · 928 Bytes
/
MaxMinArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class MaxMinArray {
public static void main(String [] args){
int vetor[] = {9, 0, 4, 2, 3, 8, 7, 1, 6, 5};
MaxMinRecursivo(vetor, vetor[0], vetor[0], 0);
System.out.println("---------");
MaxMinIterativo(vetor);
}
public static void MaxMinRecursivo(int vetor[], int max, int min, int indice){
if(vetor[indice] > max)
max = vetor[indice];
if(vetor[indice] < min)
min = vetor[indice];
if(indice < vetor.length-1)
MaxMinRecursivo(vetor, max, min, indice+1);
else{
System.out.println("Max : " + max);
System.out.println("Min : " + min);
}
}
public static void MaxMinIterativo(int vetor[]){
int max = vetor[0], min = vetor[0];
if( vetor.length > 1){
for (int i = 0; i < vetor.length; i++) {
if(vetor[i] > max)
max = vetor[i];
else if(vetor[i] < min)
min = vetor[i];
}
}
System.out.println("Max : " + max);
System.out.println("Min : " + min);
}
}