Last active
October 20, 2024 23:19
-
-
Save lcmuniz/7c11cc2ec400f513974cc48f1bf05427 to your computer and use it in GitHub Desktop.
Função para ordenar um vetor em ordem crescente.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void ordena(int vetor[], int tamanho) | |
{ | |
for (int i = 0; i < tamanho; i++) | |
{ | |
for (int j = i; j < tamanho; j++) | |
{ | |
if (vetor[i] > vetor[j]) | |
{ | |
int temp = vetor[i]; | |
vetor[i] = vetor[j]; | |
vetor[j] = temp; | |
} | |
} | |
} | |
} | |
void mostrarVetor(int vetor[], int tamanho) | |
{ | |
for (int i = 0; i < tamanho; i++) | |
{ | |
printf("%d ", vetor[i]); | |
} | |
} | |
int main() | |
{ | |
int vetor[] = {5,6,2,4,3,1}; | |
int tamanho = sizeof(vetor) / sizeof(int); | |
printf("Vetor antes da ordenação: "); | |
mostrarVetor(vetor, tamanho); | |
printf("\n"); | |
ordena(vetor, tamanho); | |
printf("Vetor depois da ordenação: "); | |
mostrarVetor(vetor, tamanho); | |
printf("\n"); | |
} |
for (int i = 0; i < tamanho - 1; i++) { for (int j = i; j < tamanho - 1; j++) // Se o tamanho aqui for - 1 o ultimo valor não entra na validação, retirando ele " - 1 " o codigo funciona perfeitamente. { if (vetor[i] > vetor[j]) { int temp = vetor[i]; vetor[i] = vetor[j]; vetor[j] = temp; } } } }
você tem razão. é que na versão anterior a comparação era i <= tamanho - 1 e mudou para i < tamanho e esqueci de tirar o -1. obg.
de nada !! =)
Em sex., 26 de ago. de 2022 às 11:53, Luiz Carlos Muniz <
***@***.***> escreveu:
… ***@***.**** commented on this gist.
------------------------------
for (int i = 0; i < tamanho - 1; i++) { for (int j = i; j < tamanho - 1;
j++) // Se o tamanho aqui for - 1 o ultimo valor não entra na validação,
retirando ele " - 1 " o codigo funciona perfeitamente. { if (vetor[i] >
vetor[j]) { int temp = vetor[i]; vetor[i] = vetor[j]; vetor[j] = temp; } }
} }
você tem razão. é que na versão anterior a comparação era i <= tamanho - 1
e mudou para i < tamanho e esqueci de tirar o -1. obg.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/7c11cc2ec400f513974cc48f1bf05427#gistcomment-4280802>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AYVV3MVTHMGIHVXZFY7TKETV3DK77ANCNFSM57WMWJHA>
.
You are receiving this because you commented.Message ID: <lcmuniz/ordena.
***@***.***>
--
Atenciosamente,
Anthony Felipe da Silva
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for (int i = 0; i < tamanho - 1; i++)
{
for (int j = i; j < tamanho - 1; j++) // Se o tamanho aqui for - 1 o ultimo valor não entra na validação, retirando ele " - 1 " o codigo funciona perfeitamente.
{
if (vetor[i] > vetor[j])
{
int temp = vetor[i];
vetor[i] = vetor[j];
vetor[j] = temp;
}
}
}
}