Pregunta

Estoy entramado para un examen que será de unos algoritmos de ordenación. Un amigo me dio el código sobre la LSD Radix Clasificación, y yo no entiendo por qué él está usando los números 96,97 y 64? He leído un par de cosas sobre la LSD Radix sort, pero no entiendo cómo funciona.

public class LSDRadix {
    private static String[] list;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine().trim());

        int size=0;
        list =new String[n];

        for(int i=0; i<n; i++){
            list[i]= sc.nextLine();

            if(size < list[i].length()){
                size = list[i].length();
            }
        }
        sort(size);

        for(int j=0; j<n;j++)
            System.out.println(list[j]);
    }

    private static void sort(int sizes){
        int numChars = 58;
        String [] aux = new String[list.length];
        int[] counter;

        for(int i=sizes-1; i>=0 ;i--){       
            counter = new int[numChars];

            for(int j=0; j<list.length; j++){
                if(list[j].length() > i){
                    if(list[j].charAt(i) >= 97)
                        counter[list[j].charAt(i)-96]++;
                    else
                        counter[list[j].charAt(i)-64]++;
                }else{
                    counter[0]++;
                }
            }

            for(int j=0; j<numChars-1; j++){
                counter[j+1] += counter[j]; 
            }

            for(int j=list.length-1; j>=0; j--){
                if(list[j].length() > i){
                    int pos;
                    if(list[j].charAt(i) >= 97){
                        pos = list[j].charAt(i)-96;
                    }else{
                        pos = list[j].charAt(i)-64;
                    }
                    aux[counter[pos]-1] = list[j];
                    counter[pos]--;
                }else{
                    aux[counter[0]-1] = list[j];
                    counter[0]--;
                }
            }

            for(int j=0; j<list.length; j++){
                list[j] = aux[j];
            }
        }   
    }
}
¿Fue útil?

Solución

97 es el valor ASCII de la letra 'a'. Si el carácter que se está probando es una letra minúscula, restar 96 desde su valor ASCII dará un número entre 1 y 26.

De lo contrario, el personaje se supone que es una letra mayúscula. 65 es el valor ASCII para la letra 'A', de modo restando 64 volverá a dar un valor entre 1 y 26.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top