Baixe o app para aproveitar ainda mais
Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <time.h> #define TAM 10000 using namespace std; int RandomInteger(int low, int high) { int k; srand( (unsigned)time(NULL) ); k = (rand() % high) + low; return k; } void bubbleSort (int v[TAM]) { int a, b, aux,z; for (a=TAM-1; a>=1; a--) { for (b=0; b<a; b++) { if (v[b]>v[b+1]) { aux = v[b]; v[b] = v[b+1]; v[b+1] = aux; } } } } void swapping(int &a, int &b) { //troque o conteúdo de a e b int temp; temp = a; a = b; b = temp; } void selectionSort(int *array, int size) { int i, j, imin; for(i = 0; i<size-1; i++) { imin = i; //obter índice de dados mínimos for(j = i+1; j<size; j++) if(array[j] < array[imin]) imin = j; //colocando na posição correta swap(array[i], array[imin]); } } int main(){ clock_t t; //variável para armazenar tempo int vetorCasoMedio[TAM],vetorMelhorCaso[TAM],vetorPiorCaso[TAM]; //vetor com 10000 posições int p, r, a,num; p = 0; r = TAM; //geração dos valores do vetor for(a = 0; a < TAM; a++){ vetorCasoMedio[a] = RandomInteger(0, TAM); } for(a = 0; a < TAM; a++){ vetorMelhorCaso[a] = a+1; } for(a = TAM; a > 0; a--){ vetorPiorCaso[a] = a--; } t = clock(); //armazena tempo //ESCOLHA O VETOR DE MELHOR, PIOR OU MEDIO CASO E SELECIONE O ALGORITMO DESEJADO //bubbleSort(vetorMelhorCaso); //bubbleSort(vetorCasoMedio); bubbleSort(vetorPiorCaso); //selectionSort(vetorMelhorCaso, TAM); //selectionSort(vetorCasoMedio, TAM); //selectionSort(vetorPiorCaso, TAM); t = clock() - t; //tempo final - tempo inicial //imprime o tempo na tela printf("Tempo de execucao: %lf", ((double)t)/((CLOCKS_PER_SEC/1000))); //conversão para double }
Compartilhar