Guest User

Busca e ordenação de vetor

a guest
Aug 26th, 2016
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.76 KB | None | 0 0
  1. //Diretivas de pré processamento
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <time.h>
  7. #define max 10
  8.  
  9. int busca_sequencial(int *, int *);
  10. int busca_binaria(int *, int *);
  11. void quick_crescente(int *, int, int);
  12. void quick_decrescente(int *, int, int);
  13. void bubble_crescente(int *, int);
  14. void bubble_decrescente(int *, int);
  15.  
  16. //Função principal, onde o vetor é inicializado e se entra com uma valor
  17. int main() {
  18.  
  19.     srand(time(NULL));
  20.  
  21.     //Embelezamento da tela
  22.     system("color 70");
  23.  
  24.     //Declaração das variaveis
  25. int vetor[max], i, j, valor, resultado;
  26.  
  27.     printf("Vetores: ");
  28.     //Lê o vetor, ordenando-o de forma randômica
  29.     for (i = 0; i<max; i++)
  30.     {
  31.         vetor[i] = rand() % 9;
  32.         for (j = 0; j<i; j++)
  33.             if (vetor[i] == vetor[j])
  34.                 vetor[i] = rand() % 9;
  35.         printf("| %d ", vetor[i]);
  36.  
  37.     }
  38.     printf("|\n\n");
  39.  
  40.     //Busca do vetor
  41.     printf("Entre com um dos numeros acima: ");
  42.     scanf("%d", &valor);
  43.  
  44.     //Redireciona a busca para uma função
  45.     resultado = busca_sequencial(&vetor[0], &valor);
  46.     //resultado = busca_binaria(&vetor[0], &valor);
  47.  
  48.     //Tras o resultado da posição
  49.     if (resultado == -1)
  50.         printf("Vetor nao encontrado\n");
  51.     else
  52.         printf("\nPosicao do vetor: %d\nVetor digitado: %d\n\n", resultado, valor);
  53.  
  54.     printf("Tecle [Enter] para ordenar o vetor.\n\n");
  55.     _getch();
  56.     printf("Comecando a ordenacao do vetor...\n\n");
  57.  
  58.     //Chama e pula para a função de ordenação do vetor
  59.     //quick_crescente(&vetor[0], 0, max - 1);
  60.     quick_decrescente(&vetor[0], 0, max - 1);
  61.     //bubble_crescente(&vetor[0], max - 1);
  62.     //bubble_decrescente(&vetor[0], max - 1);
  63.  
  64.     printf("Vetores ordenados: | ");
  65.     //Lê o vetor já ordenado
  66.     for (i = 0; i <= max; i++) {
  67.         printf("%d | ", vetor[i]);
  68.     }
  69.     printf("|\n\n");
  70.  
  71.     system("pause");
  72.     return 0;
  73. }
  74.  
  75. //Busca de vetor pela forma sequencial
  76. int busca_sequencial(int *vetor, int *valor)
  77. {
  78.     int maior, i, po = 0;
  79.  
  80.     maior = *vetor;
  81.  
  82.  
  83.  
  84.     for (i = 0; i < max; i++) {
  85.         if (vetor[i] == *valor) {
  86.             maior = vetor[i];
  87.             po = i;
  88.             return po;
  89.         }
  90.     }
  91. }
  92.  
  93. //Busca de vetor pela forma binária
  94. int busca_binaria(int *pt, int *val)
  95. {
  96.     int inf = 0, sup = max , meio;
  97.  
  98.     do {
  99.         meio = (sup + inf) / 2;
  100.         if (*val == pt[meio])
  101.             return meio;
  102.         if (*val < pt[meio])
  103.             sup = meio - 1;
  104.         else
  105.             inf = meio + 1;
  106.     } while (inf <= sup);
  107.     return -1;
  108. }
  109.  
  110. //Ordenação crescente por Quicksort
  111. void quick_crescente(int *pt, int inicio, int fim) {
  112.  
  113.     int pivo, aux, i, j, meio;
  114.  
  115.     //Prepara as variaveis para a ordenação
  116.     i = inicio;
  117.     j = fim;
  118.  
  119.     meio = (int)((i + j) / 2);
  120.     pivo = pt[meio];
  121.  
  122.     do {
  123.         //Verifica se o inicio e o fim já chegaram no meio para poder terminarem
  124.         while (pt[i] < pivo)
  125.             i = i + 1;
  126.         while (pt[j] > pivo)
  127.             j = j - 1;
  128.  
  129.         //Troca as posições dos numeros do vetor, caso necessario | aux = Variavel auxiliar para guardar o vetor por um instante
  130.         if (i <= j) {
  131.             aux = pt[i];
  132.             pt[i] = pt[j];
  133.             pt[j] = aux;
  134.             i = i + 1;
  135.             j = j - 1;
  136.         }
  137.         //Condição de existencia, quando J e I forem iguais ou j > i o algoritmo pula fora
  138.     } while (j > i);
  139.  
  140.     if (j > inicio)
  141.         quick_crescente(pt, inicio, j);
  142.     if (i < fim)
  143.         quick_crescente(pt, i, fim);
  144. }
  145.  
  146. //Ordenação decrescente por Quicksort
  147. void quick_decrescente(int *pt, int inicio, int fim) {
  148.  
  149.     int pivo, aux, i, j, meio;
  150.  
  151.     //Prepara as variaveis para a ordenação
  152.     i = inicio;
  153.     j = fim;
  154.  
  155.     meio = (int)((i + j) / 2);
  156.     pivo = pt[meio];
  157.  
  158.     do {
  159.         //Verifica se o inicio e o fim já chegaram no meio para poder terminarem
  160.         while (pt[i] > pivo)
  161.             i = i - 1;
  162.         while (pt[j] < pivo)
  163.             j = j + 1;
  164.  
  165.         //Troca as posições dos numeros do vetor, caso necessario | aux = Variavel auxiliar para guardar o vetor por um instante
  166.         if (i <= j) {
  167.             aux = pt[i];
  168.             pt[i] = pt[j];
  169.             pt[j] = aux;
  170.             i = i + 1;
  171.             j = j - 1;
  172.         }
  173.         //Condição de existencia, quando J e I forem iguais ou j > i o algoritmo pula fora
  174.     } while (j > i);
  175.  
  176.     if (j < inicio)
  177.         quick_decrescente(pt, inicio, j);
  178.     if (i > fim)
  179.         quick_decrescente(pt, i, fim);
  180. }
  181.  
  182. //Bubblesort em ordenação crescente
  183. void bubble_crescente(int *pt, int tam){
  184.  
  185.     int i,j,r,aux;
  186.     for(i=tam-1; i >= 1; i--)
  187.     {
  188.         for(j=0; j < i ; j++)
  189.         {
  190.             if(pt[j] > pt[j+1])
  191.             {
  192.                 aux = pt[j];
  193.                 pt[j] = pt[j+1];
  194.                 pt[j+1] = aux;
  195.             }
  196.         }
  197.     }
  198. }
  199.  
  200. //Ordenação decrescente por Bubblesort
  201. void bubble_decrescente(int *pt, int tam){
  202.  
  203.     int i,j,r,aux;
  204.     for(i=0; i <= tam; i++)
  205.     {
  206.         for(j=i; j >= 0 ; j--)
  207.         {
  208.             if(pt[j] < pt[j+1])
  209.             {
  210.                 aux = pt[j];
  211.                 pt[j] = pt[j+1];
  212.                 pt[j+1] = aux;
  213.             }
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment