0

pessoal estou com uma duvida, como fazer shell sort inverso decrescente.

private static void shellsort(int v[], int n) {
    int i, j, aux, h = 1;
    do
        h = 3 * h + 1;
    while (h < n);
    do {
        h /= 3;
        for (i = h; i < n; i++) {
            aux = v[i];
            j = i - h;
            while (j >= 0 && aux < v[j]) {
                v[j + h] = v[j];
                j -= h;
            }
            v[j + h] = aux;
        }
    } while (h > 1);
}
3
  • Olá, o que tem de errado com o exemplo que aí tens ?
    – Edilson
    8/04/2017 às 21:48
  • no exemplo ele faz em ordem crescente, e eu gostaria na ordem decrescente =( Vetor[995]= 99486 Vetor[996]= 99492 Vetor[997]= 99583 Vetor[998]= 99701 Vetor[999]= 99765 8/04/2017 às 21:54
  • Para mudar de crescente para decrescente basta mudar a condicao aux < v[j] para aux > v[j]
    – brow-joe
    9/04/2017 às 0:47

1 Resposta 1

1

Boa noite, para mudar de crescente para decrescente no seu código, basta alterar a condição aux < v[j] para aux > v[j]

por exemplo:

private static void shellsort(int v[], int n, boolean crescente) {
    int i, j, aux, h = 1;
    do
        h = 3 * h + 1;
    while (h < n);
    do {
        h /= 3;
        for (i = h; i < n; i++) {
            aux = v[i];
            j = i - h;
            if (crescente) {
                while (j >= 0 && aux < v[j]) {
                    v[j + h] = v[j];
                    j -= h;
                }
            } else {
                while (j >= 0 && aux > v[j]) {
                    v[j + h] = v[j];
                    j -= h;
                }
            }
            v[j + h] = aux;
        }
    } while (h > 1);
}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .