alphasil Posted May 17, 2012 at 03:41 PM Report #456428 Posted May 17, 2012 at 03:41 PM 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
bsccara Posted May 17, 2012 at 03:52 PM Report #456434 Posted May 17, 2012 at 03:52 PM 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);
alphasil Posted May 17, 2012 at 03:54 PM Author Report #456436 Posted May 17, 2012 at 03:54 PM (edited) Assim dá-me 51 69 27 86 0 1 2 3 * Não tenho erros new warnings Edited May 17, 2012 at 03:56 PM by alphasil gmc11
anolsi Posted May 17, 2012 at 03:57 PM Report #456438 Posted May 17, 2012 at 03:57 PM 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 1 Report "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!
KTachyon Posted May 17, 2012 at 04:15 PM Report #456449 Posted May 17, 2012 at 04:15 PM (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 May 17, 2012 at 04:16 PM 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
anolsi Posted May 17, 2012 at 04:20 PM Report #456453 Posted May 17, 2012 at 04:20 PM 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!
alphasil Posted May 17, 2012 at 04:49 PM Author Report #456462 Posted May 17, 2012 at 04:49 PM O erro estava aqui for (j = 1; j <= i; j++) e no prototipo bubbleSort(num, 4) Já está a funcionar, Obrigado a todos gmc11
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now