Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define max_Size 100
- #define max_value 100
- #include<stdlib.h>
- #include <time.h>
- using namespace std;
- void ordenar(int [], int);
- int maximo(int []);
- int busquedaBinaria(const int [], int );
- int busquedaLineal(int [], int );
- int i, cont;
- int main()
- {
- int data[max_value];
- srand(time(NULL));
- for(int c=0; c<max_value; c++){
- int num=1+rand()%(1000-1);
- data[c]=num;
- }
- //Ordenar lso primeros 4 numeros de un arreglo
- ordenar(data,4);
- cout<<"Ordenar 4 numeros"<<endl;
- for (i=0; i<max_value;i++)
- cout<<data[i]<<endl;
- cout<<endl<<"El numero maximo del arreglo anterior es:"
- <<maximo(data)<<endl;
- ordenar(data,100);
- cout<<endl<<"Busqueda Lineal: "
- <<busquedaLineal(data,13)<<" Intercambios"<<endl;
- cout<<endl<<"Busqueda Binaria: "
- <<busquedaBinaria(data, 25)<<" Intercambios"<<endl;
- return 0;
- }
- void ordenar(int arreglo[], int num){
- for (int i=0; i<num;i++) //K O(n)
- for (int j=i+1;j<num;j++) //K O(n)
- if (arreglo[i]>arreglo[j]) {
- int temp=arreglo[i];
- arreglo[i] = arreglo[j];
- arreglo[j] = temp;
- }
- }
- int maximo(int arreglo[]){
- int maxi;
- maxi=arreglo[0];
- for(int i=1; i<max_value; i++)
- if (maxi<arreglo[i])
- maxi=arreglo[i];
- return maxi;
- }
- int busquedaLineal(int arreglo[], int value){
- cont=0;
- for (int i = 0; i<max_value ; i++){
- if (arreglo[i] == value) return cont;
- cont++;
- }
- return cont;
- }
- int busquedaBinaria(const int arreglo[], int clave){
- int Iarriba = max_value-1;
- int Iabajo = 0;
- int Icentro;
- cont=0;
- while (Iabajo <= Iarriba)
- {
- Icentro = (Iarriba + Iabajo)/2;
- if (arreglo[Icentro] == clave)
- return cont;
- else
- if (clave < arreglo[Icentro])
- Iarriba=Icentro-1;
- else
- Iabajo=Icentro+1;
- cont++;
- }
- return cont;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement