import java.io.*; class Invertir { public static void main(String arg[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Ingrese numero de elementos : "); int n = Integer.parseInt(in.readLine()); int a[] = new int[n]; /* lectura del arreglo por teclado */ int i; for(i = 0; i < a.length; i++) { System.out.print("Ingrese elemento " + (i + 1) + " : "); a[i] = Integer.parseInt(in.readLine()); } System.out.print("Arreglo ingresado : "); for(i = 0; i < a.length; i++) System.out.print(a[i] + "\t"); System.out.print("\nIngrese elemento a insertar : "); int x = Integer.parseInt(in.readLine()); int tmp; i = 0; while(a[i] < x) i++; /* se busca posicion i a insertar */ while(i < a.length) { tmp = a[i]; /* se guarda lo que que hay en posicion i */ a[i] = x; /* se asigna el elemento a insertar x a la posicion i */ x = tmp; /* el nuevo elemento a insertar es tmp */ i++; /* como x tiene que insertarse en la siguiente posicion se incrementa el indice */ } System.out.print("Arreglo final : "); for(i = 0; i < a.length; i++) System.out.print (a[i] + "\t"); System.out.println(); } }