daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jul 12th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5.  
  6. #define TAM 1000
  7.  
  8. void SelectionSort_1();
  9.  
  10.  
  11. int main (){        
  12.  
  13.     int iCont, jCont, aux = 0, vetor[TAM];
  14.  
  15.     FILE *ent;
  16.  
  17.     ent = fopen("teste.txt", "r");
  18.  
  19.     if(ent == NULL){
  20.         printf("Erro! Nao consegui abrir o arquivo...n");
  21.         exit(1);
  22.     }
  23.  
  24.     for(iCont = 0; iCont < TAM; iCont++){
  25.         //printf("Lendo posicao %dn", iCont);
  26.         if(fscanf(ent, "%d", &vetor[iCont]) == EOF){
  27.                 setbuf(stdin, NULL);
  28.             printf("Opa, tentei ler alem do final do arquivo...n");
  29.             break;
  30.         }
  31.     }
  32.  
  33.     SelectionSort_1(vetor);
  34.  
  35.  
  36.     fclose( ent );
  37.  
  38.     printf("nnOrdenado: nn");
  39.     for(iCont = 0; iCont < TAM; iCont++){
  40.         printf("%d ", vetor[iCont]);
  41.     }
  42.  
  43.     printf("nnnn");
  44.  
  45.     return 0;
  46. }
  47.  
  48. void SelectionSort_1(int vetor[]){
  49.     int iCont, jCont, min, aux = 0;
  50.  
  51. struct timeval  tv1, tv2;
  52. gettimeofday(&tv1, NULL);
  53.  
  54.     for(iCont = 0; iCont < TAM - 1; iCont++){
  55.         min = iCont;
  56.         for(jCont = iCont + 1; jCont < TAM; jCont++){
  57.             if(vetor[jCont] < vetor[min])
  58.                 min = jCont;
  59.         }
  60.     if(vetor[iCont] != vetor[min]){
  61.         aux = vetor[iCont];
  62.         vetor[iCont] = vetor[min];
  63.         vetor[min] = aux;
  64.      }
  65.   }
  66.  
  67. gettimeofday(&tv2, NULL);
  68. printf ("Total time = %.8f secondsn",
  69.          (double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
  70.          (double) (tv2.tv_sec - tv1.tv_sec));
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top