Informática, perguntado por c307423d65, 4 meses atrás

Como construir um algoritmo que receba 3 números e exiba eles em ordem crescente? Resposta da lógica do problema em português estruturado.

Soluções para a tarefa

Respondido por HeinzAry
0

Resposta:

Como tu pediste apenas três números, podemos fazer isto de forma bem simples usando o Bubble Sort, que é um algoritmo de ordenação(sorting).

Ele é o algoritmo mais simples que existe, funcionando ao trocar, repetidamente, os termos adjacentes se estes estiverem na ordem errada.

Fiz rapidamente a implementação em C para que possas ver como funciona, e estudar o algoritmo. Acredito que esta seja a linguagem que estás lidando, senão, uma parecida a esta.

#include <stdio.h>

#include <stdlib.h>

void bubbleSort(size_t array_length, int* array);

#define TEST_ARRAY_SIZE 3

int main(void) {

       int testArray[TEST_ARRAY_SIZE] = {

               3,

               5,

               1,

       };

       bubbleSort(TEST_ARRAY_SIZE, testArray);

       printf("testArray[] = {\n");

       for (int i = 0; i < TEST_ARRAY_SIZE; ++i) {

               printf("\t[%d] = %d\n", i, testArray[i]);

       }

       printf("}\n");

       return EXIT_SUCCESS;

}

void bubbleSort(size_t array_length, int* array) {

       for (int i = 0; i < array_length - 1; ++i) {

               for (int j = 0; j < (array_length - i - 1); ++j) {

                       if (array[j] > array[j + 1]) {

                               array[j] ^= array[j + 1];

                               array[j + 1] ^= array[j];

                               array[j] ^= array[j + 1];

                       }

               }

       }

}

Perguntas interessantes