-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.c
47 lines (38 loc) · 1.09 KB
/
BubbleSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Implemente e teste o algoritmo bubble Sort.
//Vivian Machado Santos
#include <stdio.h>
#include<stdlib.h>
void BubbleSort(int vetor[], int tam){
int memoria, troca, i, j;
troca=1; //A variável "troca" será a verificação da troca em cada passada
for(j=tam-1; (j>=1) && (troca==1); j--)
{
troca=0; //Se o valor continuar 0 na próxima passada quer dizer que não houve troca e a função é encerrada.*/
for(i=0; i<j; i++)
{
if(vetor[i]>vetor[i+1])
{
memoria=vetor[i];
vetor[i]=vetor[i+1];
vetor[i+1]=memoria;
troca=1; //Se houve troca, "troca" recebe 1 para continuar rodando.*/
}
}
}
for (i = 0; i < tam; i++)
{
printf ("%d ",vetor[i]);//Imprime o vetor ordenado
}
printf ("\n");}
int main () {
int max, i;
printf("Digite o tamanho do vetor ");
scanf ("%d",&max);
int vetor[max];
for (i = 0; i < max; i++)
{
printf("Digite o %d numero ", i+1);
scanf ("%d",&vetor[i]);
}
BubbleSort(vetor, max);
}