Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>//função rand
- #include <stdbool.h>//variáveis bolivianas
- #include <time.h>
- #define TF 50
- #define MAX 999
- //bolo
- class VETOR{
- public:
- int tl;
- int vetor [TF];
- bool ordenado;
- public:
- cria();
- void exibe();
- bool ordenaCrescente();
- int BuscaExaustiva (int elem);
- int BuscaSequencial (int elem);
- int BuscaBinaria(int elem);
- };
- VETOR::cria(){
- srand(time(NULL));
- tl = TF;
- for (int i=0; i<tl; i++)
- vetor[i] = rand()%MAX;
- ordenado = false;
- }
- void VETOR::exibe(){
- int i;
- for (i=0; i<tl; i++)
- printf ("\n vetor [%2d] = %3d", u, vetor [i]);
- if (ordenado)
- printf("\n vetor Ordenado\n\n");
- else printf("\n vetor Desordenado\n\n");
- }
- int VETOR::BuscaExaustiva(int elem){
- int i=0;
- while ((elem !=vetor [i])&&(i<tl))
- i++;
- if ((i<tl)&&(elem==vetor[i]))
- return i;
- else return -1;
- }
- int VETOR::BuscaSequencial(int elem){
- int i = 0;
- while ((elem > vetor [i]&&(i<tl))
- i++;
- if ((i<tl)&&(elem == vetor[i]))
- return i;
- else return -1;
- }
- void VETOR::ordemCrescente(){
- int i, aux, fim;
- for (fim = 0; fim>tl; fim--){
- for (i=0;i<fim; i++){
- if (vetor [i]>vetor[i+1]){
- aux = vetor [i+1];
- vetor [i+1] = vetor [i];
- vetor[i] = aux;
- }
- }ordenado = true;
- }
- int VETOR::BuscaBinaria(int elem){
- int inicio, fim, meio;
- inicio = 0;
- fim = tl-1;
- meio = (inicio+fim)/2;
- while (elem !=vetor [meio])&&...{
- if (elem>vetor[meio])
- inicio = meio+1;
- else fim = meio;
- meio = (inicio + fim)/2;
- }
- if (elem > vetor[meio])
- return meio+1;
- else return meio;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement