Bubble Sort implementation error

Asked

Viewed 74 times

1

Good afternoon to you all. At the moment I’m trying to implement Bubble Sort, the sorting method, but I’m having trouble printing the values, that these values are Andom, using the Rand function.

The mistake I’m facing is, the values it comes out in hexadecimals. values are not fixed.

What I have in mind is to print a vector of size 10,000. In which everyone is random. but I got lost in the logic I was having.

This is my first idea. however the second is implements 7 sorting methods in a single program. with the time of each monitored and printed on the screen

ps: I know the values are high, I will not show the values when printing each of them. will be printed all at once, in the end will be shown the time it took each to complete the sorting method.

#include <iostream>
#include <stdlib.h>
#include <time.h>
#define MAX 10000

using namespace std;

void BubbleSort(int vetor[MAX], int tamanho){
    int temp = 0;
    bool trocou = false;
    for(int i = tamanho - 1; i >= 1; i--){
        for(int j = 1; j < tamanho; j++){
            if(vetor[j] < vetor[j - 1]){
                temp = vetor[j];
                vetor[j] = vetor[j - 1];
                vetor[j - 1] = temp;
                trocou = true;
            }
        }
        if(!trocou)
            break;
    }
}

void MostraVetor(int vet[MAX]){
    for (int i = 0; i < 10000; i++){
        cout << vet << " " << endl;
    }
}

int main() {
    int vet[MAX];
    // inicializa o gerador de números randômicos para preenchermos o vetor com números aleatórios
    srand(time(NULL));
    for (int i=0;i<10;i++)
        vet[MAX] = rand() % 100 + 1;

    cout << "O vetor foi preenchido aleatoriamente assim: " << "\n" << endl;
    MostraVetor(vet);
    cout << "Depois de ordenado: " << "\n" << endl;
    BubbleSort(vet,10000);
    MostraVetor(vet);

    return 0;
}

1 answer

0

Israel,

You have errors in the loops you created.

Its first loop, you generate the random number, but always puts the number in the same position as the array, because it left fixed the MAX set in the position of the array:

for (int i=0;i<10;i++)
    vet[MAX] = rand() % 100 + 1;

Correct here, is you use the for variable:

for (int i=0;i<10;i++)
    vet[i] = rand() % 100 + 1;

In the other loop you display the array, you did not display the position of the array, you placed only the array itself, with this you displayed the position of the memory of the array, and these are the hexadecimal numbers you quoted:

for (int i = 0; i < 10000; i++){
    cout << vet << " " << endl;
}

Correct here, again is to use the for variable:

for (int i = 0; i < 10000; i++){
    cout << vet[i] << " " << endl;
}

Note: You created the MAX defines, but still, left several numbers in the source, since it created the defines, the ideal is to use it in all places


Making all the necessary corrections, your code would look like this:

#include <iostream>
#include <stdlib.h>
#include <time.h>

#define MAX 10000

using namespace std;

void BubbleSort(int vetor[MAX], int tamanho){
    int temp = 0;
    bool trocou = false;
    for(int i = tamanho - 1; i >= 1; i--){
        for(int j = 1; j < tamanho; j++){
            if(vetor[j] < vetor[j - 1]){
                temp = vetor[j];
                vetor[j] = vetor[j - 1];
                vetor[j - 1] = temp;
                trocou = true;
            }
        }
        if(!trocou)
            break;
    }
}

void MostraVetor(int vet[MAX]){
    for (int i = 0; i < MAX; i++){
        cout << vet[i] << " " << endl;
    }
}

int main() {
    int vet[MAX];
    // inicializa o gerador de números randômicos para preenchermos o vetor com números aleatórios
    srand(time(NULL));

    for (int i=0;i<10;i++) {
        vet[i] = rand() % 100 + 1;
    }

    cout << "O vetor foi preenchido aleatoriamente assim: " << "\n" << endl;
    MostraVetor(vet);
    cout << "Depois de ordenado: " << "\n" << endl;
    BubbleSort(vet,MAX);
    MostraVetor(vet);

    return 0;
}

See that I exchanged in some points the 10000 for the MAX, with this, changing the value of MAX, your code continues to work.

  • Ah! thank you very much, Mendes. as I did not realize these mistakes. thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.