Advertisement
Guest User

Untitled

a guest
May 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.33 KB | None | 0 0
  1. #include   <limits.h>
  2. #include   <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. /*#include   <openssl/rand.h>*/
  7. #define MAXTAM 1000
  8.  
  9. typedef long TipoChave;
  10. typedef struct TipoItem {
  11.   TipoChave Chave;
  12.   /* outros componentes */
  13. } TipoItem;
  14.  
  15. typedef int TipoIndice;
  16. typedef TipoItem TipoVetor[MAXTAM + 1];
  17. /* MAXTAM+1 por causa da sentinela em Insercao */
  18. TipoVetor A;
  19. TipoIndice i, n;
  20.  
  21. void Selecao(TipoItem *A, TipoIndice n)
  22. { TipoIndice i, j, Min;
  23.   TipoItem x;
  24.   for (i = 1; i <= n - 1; i++)
  25.     { Min = i;
  26.       for (j = i + 1; j <= n; j++)
  27.         if (A[j].Chave < A[Min].Chave) Min = j;
  28.       x = A[Min]; A[Min] = A[i]; A[i] = x;
  29.     }
  30. }
  31.  
  32. void Insercao(TipoItem *A, TipoIndice n)
  33. { TipoIndice i, j;
  34.   TipoItem x;
  35.   for (i = 2; i <= n; i++)
  36.     { x = A[i];  j = i - 1;
  37.       A[0] = x;  /* sentinela */
  38.       while (x.Chave < A[j].Chave)
  39.         { A[j+1] = A[j];  j--;
  40.         }
  41.       A[j+1] = x;
  42.     }
  43. }
  44.  
  45. void Bolha(TipoItem *A, TipoIndice n)
  46. { int i, j;  int h = 1;
  47.   TipoItem x;
  48.   do h = h * 3 + 1; while (h < n);
  49.   do
  50.     { h /= 3;
  51.       for (i = h + 1; i <= n; i++)
  52.         { x = A[i];  j = i;
  53.           while (A[j - h].Chave > x.Chave)
  54.             { A[j] = A[j - h];  j -= h;
  55.               if (j <= h) goto L999;
  56.             }
  57.             L999: A[j] = x;
  58.         }
  59.     } while (h != 1);
  60. }
  61.  
  62.  
  63. void Particao(TipoIndice Esq, TipoIndice Dir,
  64.               TipoIndice *i, TipoIndice *j, TipoItem *A)
  65. { TipoItem x, w;
  66.   *i = Esq;  *j = Dir;
  67.   x = A[(*i + *j) / 2]; /* obtem o pivo x */
  68.   do
  69.     { while (x.Chave > A[*i].Chave) (*i)++;
  70.       while (x.Chave < A[*j].Chave) (*j)--;
  71.       if (*i <= *j)
  72.       { w = A[*i]; A[*i] = A[*j]; A[*j] = w;
  73.         (*i)++; (*j)--;
  74.       }
  75.     } while (*i <= *j);
  76. }
  77.  
  78. void Ordena(TipoIndice Esq, TipoIndice Dir, TipoItem *A)
  79. { TipoIndice i, j;
  80.   Particao(Esq, Dir, &i, &j, A);
  81.   if (Esq < j) Ordena(Esq, j, A);
  82.   if (i < Dir) Ordena(i, Dir, A);
  83. }
  84.  
  85. void QuickSort(TipoItem *A, TipoIndice n)
  86. { Ordena(1, n, A); }
  87.  
  88. void Refaz(TipoIndice Esq, TipoIndice Dir, TipoItem *A)
  89. { TipoIndice i = Esq;
  90.   int j;
  91.   TipoItem x;
  92.   j = i * 2;
  93.   x = A[i];
  94.   while (j <= Dir)
  95.     { if (j < Dir)
  96.       { if (A[j].Chave < A[j+1].Chave)
  97.         j++;
  98.       }
  99.       if (x.Chave >= A[j].Chave) goto L999;
  100.       A[i] = A[j];
  101.       i = j;  j = i * 2;
  102.     }
  103.   L999: A[i] = x;
  104. }
  105.  
  106. void Constroi(TipoItem *A, TipoIndice n)
  107. { TipoIndice Esq;
  108.   Esq = n / 2 + 1;
  109.   while (Esq > 1)
  110.     { Esq--;
  111.       Refaz(Esq, n, A);
  112.     }
  113. }
  114.  
  115. /*--Entra aqui a funcao Refaz do Programa 4.9 --*/
  116. /*--Entra aqui a funcao Constroi do Programa 4.10--*/
  117.  
  118. void Heapsort(TipoItem *A, TipoIndice n)
  119. { TipoIndice Esq, Dir;
  120.   TipoItem x;
  121.   Constroi(A, n);  /* constroi o heap */
  122.   Esq = 1;  Dir = n;
  123.   while (Dir > 1)
  124.     { /* ordena o vetor */
  125.       x = A[1];  A[1] = A[Dir];  A[Dir] = x;  Dir--;
  126.       Refaz(Esq, Dir, A);
  127.     }
  128. }
  129.  
  130. void Imprime(TipoItem *V, TipoIndice n)
  131. { for (i = 1; i <= n; i++)
  132.     printf("%li ", V[i].Chave);  printf("\n");
  133. }
  134.  
  135. void Copia(TipoItem *Fonte, TipoItem *Destino, TipoIndice n)
  136. { for (i = 1; i <= n; i++)
  137.     Destino[i] = Fonte[i];
  138. }
  139.  
  140. void Testa(TipoItem *V, TipoIndice n)
  141. { for (i = 2; i <= n; i++) {
  142.     if (V[i].Chave < V[i-1].Chave) {
  143.       printf("ERRO: ");
  144.       Imprime(V, n);
  145.       return;
  146.     }
  147.   }
  148.   printf("OK: ");
  149.   Imprime(V, n);
  150. }
  151.  
  152. double rand0a1()
  153. { double resultado=  (double) rand()/ INT_MAX; /* Dividir pelo maior inteiro */
  154.   if(resultado>1.0) resultado= 1.0;
  155.   return resultado;
  156. }
  157.  
  158. void Permut( TipoItem *A, int n)
  159. { int i,j;
  160.   TipoItem b;
  161.  
  162.   for(i = n-1; i>0; i --)
  163.   { j = (i * rand0a1()) +1 ;
  164.     b = A[i];
  165.     A[i] = A[j];
  166.     A[j] = b;
  167.   }
  168. }
  169.  
  170. int main(int argc, char *argv[])
  171. { TipoVetor B;
  172.   n = 1000;   /*Tamanho do arranjo a ser ordenado*/
  173.  
  174.   for (i = 1; i <= n; i++)
  175.  
  176.     A[i].Chave = rand();/*gera vetor aleatorio*/
  177.  
  178.   Permut (A,n);
  179.   Copia (A,B,n);
  180.  
  181.   printf("Desordenado : ");
  182.   Imprime(A, n);
  183.  
  184.   printf("Selecao   ");
  185.   Selecao(B, n);
  186.   Testa(B, n);
  187.   Copia(A, B, n);
  188.  
  189.   printf("Insercao  ");
  190.   Insercao(B, n);
  191.   Testa(B, n);
  192.   Copia(A, B, n);
  193.  
  194.   printf("Bolha ");
  195.   Bolha(B, n);
  196.   Testa(B, n);
  197.   Copia(A, B, n);
  198.  
  199.   printf("Quicksort ");
  200.   QuickSort(B, n);
  201.   Testa(B, n);
  202.   Copia(A, B, n);
  203.  
  204.   printf("Heapsort  ");
  205.   Heapsort(B, n);
  206.   Testa(B, n);
  207.   Copia(A, B, n);
  208.   return 0;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement