package tutorialJava.capitulo4_Arrays.ejercicios.bloque11;

public class Ejercicio01_OrdenarArrayDeStrings {

	public static void main(String[] args) {
		String palabras[] = new String[] {"aves", "mamiferos", 
				"reptiles", "insectos", "anfibios"};
		
		ordenaBurbuja(palabras);
		
		for (int i = 0; i < palabras.length; i++) {
			System.out.println(palabras[i]);
		}

	}

	
	public static void ordenaBurbuja (String a[]) {
		boolean hayIntercambios;
		do {
			hayIntercambios = false;
			for (int i = 0; i < (a.length - 1); i++) {
				if (comparaCadenas(a[i], a[i+1]) == 1) {
					String aux = a[i];
					a[i] = a[i + 1];
					a[i + 1] = aux;
					hayIntercambios = true;
				}
			}
		} while (hayIntercambios == true);
	}
	
	
	
	
	
	
	
	public static int comparaCadenas(String s1, String s2) {
		
		int menorLongitud = (s1.length() < s2.length())? s1.length() : s2.length();
		
		for (int i = 0; i < menorLongitud; i++) {
			if (s1.charAt(i) < s2.charAt(i)) return -1;
			else if (s1.charAt(i) > s2.charAt(i)) return 1;
		}			
		
		if (s1.length() == s2.length()) return 0;
		return (s1.length() < s2.length())? -1 : 1;		
	}

}
