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;
}
Ah! thank you very much, Mendes. as I did not realize these mistakes. thank you very much.
– Israelwes