Informática, perguntado por BielPP, 4 meses atrás

dá pra arrumar?!
#include
#include
#include

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// A function to implement bubble sort
void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}




int main()

{
int vetor_elementos_1[10000];// vetor desordenado (medio caso)


int vetor_elementos_2[10000]; // vetor ordenado (melhor caso)

int vetor_elementos_3[10000]; // vetor totalmente desordenado (pior caso)

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



clock_t Ticks[2];
Ticks[0] = clock();


//O código a ter seu tempo de execução medido ficaria neste ponto.
bubbleSort(vetor_elementos_1,10000);




Ticks[1] = clock();
double Tempo = (Ticks[1] - Ticks[0]) * 1000.0 / CLOCKS_PER_SEC;
printf("Tempo gasto médio caso: %g ms.\n", Tempo);


for(int i = 0;i<10000;i++)
{
vetor_elementos_2[i] = vetor_elementos_1[i];
vetor_elementos_3[10000-i] = vetor_elementos_1[i];
}


clock_t Ticks1[2];
Ticks1[0] = clock();

//O código a ter seu tempo de execução medido ficaria neste ponto.
bubbleSort(vetor_elementos_2,10000);
Ticks1[1] = clock();
Tempo = (Ticks1[1] - Ticks1[0]) * 1000.0 / CLOCKS_PER_SEC;
printf("Tempo gasto melhor caso: %g ms.\n", Tempo);


clock_t Ticks2[2];
Ticks2[0] = clock();

//O código a ter seu tempo de execução medido ficaria neste ponto.
bubbleSort(vetor_elementos_3,10000);

Ticks2[1] = clock();
Tempo = (Ticks2[1] - Ticks2[0]) * 1000.0 / CLOCKS_PER_SEC;
printf("Tempo gasto pior caso: %g ms.\n", Tempo);








return 0;
}

Soluções para a tarefa

Respondido por alexandracabralg2
0

Resposta:

Explicação

jajajja

++@

A

A

A

A

Abhsjwnensjj

S

{×8÷°;^44||4":✓\@0@)]!!bbahqundnd

Respondido por diegodagoberto
0

Resposta:

Tipo de bolha

Bubble Sort é o algoritmo de classificação mais simples que funciona trocando repetidamente os elementos adjacentes se eles estiverem na ordem errada.

Exemplo:

Primeira passagem:

( 5 1 4 2 8) -> ( 1 5 4 2 8), Aqui, o algoritmo compara os dois primeiros elementos e troca desde 5> 1.

(1 5 4 2 8) -> (1 4 5 2 8), Trocar desde 5> 4

(1 4 5 2 8) -> (1 4 2 5 8), Trocar desde 5> 2

(1 4 2 5 8 ) -> (1 4 2 5 8), Agora, como esses elementos já estão em ordem (8> 5), o algoritmo não os troca.

Segunda passagem:

( 1 4 2 5 8) -> ( 1 4 2 5 8)

(1 4 2 5 8) -> (1 2 4 5 8), Trocar desde 4> 2

(1 2 4 5 8) -> (1 2 4 5 8)

(1 2 4 5 8 ) -> (1 2 4 5 8 )

Agora, o array já está ordenado, mas nosso algoritmo não sabe se está completo. O algoritmo precisa de uma passagem inteira sem nenhuma troca para saber que está classificado.

Terceira passagem:

( 1 2 4 5 8) -> ( 1 2 4 5 8)

(1 2 4 5 8) -> (1 2 4 5 8)

(1 2 4 5 8) -> (1 2 4 5 8 )

(1 2 4 5 8 ) -> (1 2 4 5 8 )

Recomendado: Resolva primeiro em “ PRÁTICA ”, antes de prosseguir para a solução.

A seguir estão as implementações do Bubble Sort.

// C++ program for implementation of Bubble sort

#include <bits/stdc++.h>

using namespace std;

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

// A function to implement bubble sort

void bubbleSort(int arr[], int n)

{

int i, j;

for (i = 0; i < n-1; i++)

// Last i elements are already in place

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

}

/* Function to print an array */

void printArray(int arr[], int size)

{

int i;

for (i = 0; i < size; i++)

cout << arr[i] << " ";

cout << endl;

}

// Driver code

int main()

{

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr)/sizeof(arr[0]);

bubbleSort(arr, n);

cout<<"Sorted array: \n";

printArray(arr, n);

return 0;

}

// This code is contributed by rathbhupendra

Perguntas interessantes