Advertisement
Guest User

algoritmos1

a guest
Mar 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #include "ordenation.h"
  6.  
  7. int main(){
  8.     int size, *array, num, busca;
  9.     double time_spent;
  10.     clock_t begin, end;
  11.  
  12.     srand(time(NULL));
  13.  
  14.     printf("Qual o tamanho do vetor que deseja ordenar? ");
  15.     scanf("%d", &size);
  16.     printf("Qual busca deseja fazer: sequencial[0] ou binaria[1]? ");
  17.     scanf("%d", &busca);
  18.  
  19.     array = (int *) malloc(sizeof(int)*size);
  20.  
  21.     for(int i = 0; i < size; i++){
  22.           array[i] = rand()%size;    //numeros de zero até size
  23.     }
  24.  
  25.     //clock no inicio
  26.     begin = clock();
  27.     //ordena
  28.     insertion_sort(array, size);
  29.     //clock no final
  30.     end = clock();
  31.  
  32.     time_spent = (double) (end - begin)/CLOCKS_PER_SEC;
  33.  
  34.     printf("O tempo para a ordenacao foi de %f", time_spent);
  35.  
  36.     num = (rand()*100)%(size*2);
  37.  
  38.     if(busca == 0){
  39.         begin = clock();
  40.         int position = linear_search(array, size, 0, num);
  41.         end = clock();
  42.  
  43.         time_spent = (double) (end - begin)/CLOCKS_PER_SEC;
  44.  
  45.         printf("\nO tempo para a busca sequencial foi de %f", time_spent);
  46.     }
  47.     else if(busca == 1){
  48.         begin = clock();
  49.         int position = binary_search(array, size, num);
  50.         end = clock();
  51.  
  52.         time_spent = (double) (end - begin)/CLOCKS_PER_SEC;
  53.  
  54.         printf("\nO tempo para a busca binaria foi de %f", time_spent);
  55.     }
  56.     else
  57.         printf("Opcao de busca invalida.");
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement