Pergunta

Estou estudando para um exame que será sobre classificar algoritmos. Um amigo me deu esse código sobre a classificação do LSD Radix, e eu não entendo por que ele está usando os números 96,97 e 64? Eu li algumas coisas sobre o tipo LSD Radix, mas não entendi como 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];
            }
        }   
    }
}
Foi útil?

Solução

97 é o valor ASCII para a letra 'A'. Se o caractere que está sendo testado for uma letra inferior, subtrair 96 do seu valor ASCII fornecerá um número entre 1 e 26.

Caso contrário, assume-se que o personagem seja uma letra superior. 65 é o valor ASCII para a letra 'A', portanto, subtrair 64 fornecerá novamente um valor entre 1 e 26.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top