Jump to content

[Resolvido] Ordenar com Bubblesort


Recommended Posts

Posted

Oi ppl;

Estou com um pequeno problema em ordenar um vetor.

Tenho um vetor de 100 que me dá 4 números random, quero pegar nestes números e ordená-los mas por alguma azelhice minha não estou a conseguir, imprime os mesmos números dados no random.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TAM 100
void LeVetor(int num[])
{
int i, n, tmp;
srand(time(NULL));
// Inicia o array
for(i = 0;i < TAM;++i)
 num[i] = i;
// Embaralha o array
for (i = TAM; i > 1;)
{
 n = rand() % i;
 --i;
 tmp = num[n];
 num[n] = num[i];
 num[i] = tmp;
}
}

void bubbleSort(int num[], int n)
{
int i, j, temp;
for (i = n; i > 0; i--)
 for (j = 1; j <= i; j++)
  if(num[j-1] > num[j])
  {
   temp = num[j-1];
   num[j-1] = num[j];
   num[j] = temp;
  }
}

int main(void)
{
int num[TAM];
int i;
LeVetor(num);
// Iteração através do array. Os números já estão random
for(i = 0;i < 4;++i)
 printf("%d\n", num[i]);
printf("\n \n");
void bubbleSort(int num[], int n);
for(i=0; i<4; i++)
 {
	  printf("%d\n",num[i]);
 }
}

Random:

83

45

7

40

Ordenação

83

45

7

40

gmc11

 

Posted

O compilador deve estar a dar avisos porque não estás a chamar a função na main; estás a redeclará-la. Em vez de :

void bubbleSort(int num[], int n);

faz:

bubbleSort(num, TAM - 1);
Posted

Repara que o BubbleSort pode ter que percorrer o array n vezes, até nada ser trocado de posição. Ou seja seria algo do género:

while (!trocado)
for(i=0;i<MAX;i++)
//...

Dá uma vista de olhos aqui: http://en.wikipedia.org/wiki/Bubble_sort#Implementation

  • Vote 1

"Nós somos o que fazemos repetidamente, a excelência não é um feito, e sim, um hábito."
Não respondo a questões por PM que possam ser colocadas no fórum!

Posted (edited)

Repara que o BubbleSort pode ter que percorrer o array n vezes, até nada ser trocado de posição. Ou seja seria algo do género:

Portanto,

while (trocado)
Edited by KTachyon

“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”

-- Tony Hoare

Posted

Portanto,

while (trocado)

Exacto tens razão. Estava a pensar bem, mas escrevi mal.

"Nós somos o que fazemos repetidamente, a excelência não é um feito, e sim, um hábito."
Não respondo a questões por PM que possam ser colocadas no fórum!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.