public class ArregloOrdenado{
public static void main(String[]args){
int N = 10;
int M[] = new int[N];
System.out.println("\n\n\n\n");
System.out.println("Determinar si un arreglo M de N elementos está ordenado y en qué tipo de orden");
System.out.println("******************************************************************************");
System.out.print("Contenido inicial");
for(int i=0; i<N; i++) {
M[i] = 10 + (int)(Math.random()*90);//se llena arreglo de forma aleatoria
System.out.print(" " + M[i]);
}
System.out.println();
verOrden(M, N);
System.out.print("Contenido inicial");
M[0] = 10 + (int)(Math.random()*10);
System.out.print(" " + M[0]);
for(int i=1; i<N; i++) {
M[i] = M[i-1] + 1 +(int)(Math.random()*10);
System.out.print(" " + M[i]);
}
System.out.println();
verOrden(M, N);
System.out.print("Contenido inicial");
M[0] = 99 - (int)(Math.random()*10);
System.out.print(" " + M[0]);
for(int i=1; i<N; i++) {
M[i] = M[i-1] - 1 - (int)(Math.random()*10);
System.out.print(" - " + M[i]);
}
System.out.println();
verOrden(M, N);
}
private static void verOrden(int M[], int N) {
boolean ASC = true;
boolean DES = true;
int ANT = M[0];
for(int i=1; i<N; i++) {
if(M[i] < ANT ) ASC = false;
if(M[i] > ANT ) DES = false;
ANT = M[i];
}
if(ASC) System.out.println("Orden ascendente\n");
if(DES) System.out.println("Orden descendente\n");
if(!ASC && !DES) System.out.println("Desordenado\n");
}
}