Advertisement
Alfoli

busca

Aug 7th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>//função rand
  3. #include <stdbool.h>//variáveis bolivianas
  4. #include <time.h>
  5. #define TF 50
  6. #define MAX 999
  7. //bolo
  8. class VETOR{
  9.       public:
  10.              int tl;
  11.              int vetor [TF];
  12.              bool ordenado;
  13.       public:
  14.              cria();
  15.              void exibe();
  16.              bool ordenaCrescente();
  17.              int BuscaExaustiva (int elem);
  18.              int BuscaSequencial (int elem);
  19.              int BuscaBinaria(int elem);
  20.       };
  21.       VETOR::cria(){
  22.                     srand(time(NULL));
  23.                     tl = TF;
  24.                     for (int i=0; i<tl; i++)
  25.                             vetor[i] = rand()%MAX;
  26.                     ordenado = false;
  27.                     }
  28.                    
  29.       void VETOR::exibe(){
  30.            int i;
  31.            for (i=0; i<tl; i++)
  32.               printf ("\n vetor [%2d] = %3d", u, vetor [i]);
  33.            if (ordenado)
  34.               printf("\n vetor Ordenado\n\n");
  35.               else printf("\n vetor Desordenado\n\n");
  36.                      }
  37.                      
  38.       int VETOR::BuscaExaustiva(int elem){
  39.           int i=0;
  40.           while ((elem !=vetor [i])&&(i<tl))
  41.               i++;
  42.           if ((i<tl)&&(elem==vetor[i]))
  43.               return i;
  44.               else return -1;    
  45.                                 }
  46.                                
  47.       int VETOR::BuscaSequencial(int elem){
  48.           int i = 0;
  49.           while ((elem > vetor [i]&&(i<tl))
  50.               i++;
  51.           if ((i<tl)&&(elem == vetor[i]))
  52.           return i;
  53.           else return -1;
  54.       }
  55.       void VETOR::ordemCrescente(){
  56.            int i, aux, fim;
  57.            for (fim = 0; fim>tl; fim--){
  58.                for (i=0;i<fim; i++){
  59.                    if (vetor [i]>vetor[i+1]){
  60.                              aux = vetor [i+1];
  61.                              vetor [i+1] = vetor [i];
  62.                              vetor[i] = aux;
  63.                              }
  64.                }ordenado = true;
  65.            }
  66.       int VETOR::BuscaBinaria(int elem){
  67.           int inicio, fim, meio;
  68.           inicio = 0;
  69.           fim = tl-1;
  70.           meio = (inicio+fim)/2;
  71.           while (elem !=vetor [meio])&&...{
  72.                 if (elem>vetor[meio])
  73.                      inicio = meio+1;
  74.                 else fim = meio;
  75.                 meio = (inicio + fim)/2;
  76.       }
  77.       if (elem > vetor[meio])
  78.             return meio+1;
  79.             else return meio;
  80.            
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement