Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <locale.h>
- #define max 10
- void preenche();
- void ordena();
- void exibe();
- main() {
- srand(time(NULL));
- int vetor[max];
- preenche(vetor);
- }
- void preenche(int vetor[])
- {
- int i;
- for (i = 0; i < max; i++)
- {
- vetor[i] = rand() % 20;
- }
- printf("Vetor sem ordenar > ");
- exibe(vetor);
- ordena(vetor);
- }
- void ordena(int vetor[])
- {
- int i, j;
- int auxiliar;
- for (i = 0; i < max; i++) {
- for (j = 0; j < max; j++)
- {
- if (vetor[i] < vetor[j])
- {
- auxiliar = vetor[i]; // auxiliar guarda valor da posição [i] do vetor
- vetor[i] = vetor[j]; // a posição [i] agora recebe o valor da posição [j]
- vetor[j] = auxiliar; // a posição [j] recebe o valor que está na auxiliar
- }
- }
- }
- printf("Vetor ordenado > ");
- exibe(vetor);
- }
- void exibe(int vetor[])
- {
- int i;
- for (i = 0; i < max; i++)
- {
- printf("%d ", vetor[i]);
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement