http://www.infount.tk/
- Código: Seleccionar todo
- #include<conio.h>
- #include<iostream>
- #include<stdlib.h>
- #include<time.h>
- #include<math.h>
- #define MAX_LLAVE 1000000
- using namespace std;
- //DECLARACION DE FUNCIONES
- void burbuja (int,int *);
- void burbujam(int, int *);
- void seleccion(int,int *);
- void shelldes(int,int *);
- void inserccion(int,int *);
- void shell(int,int *);
- void merge(int *, int, int);
- void mezcla(int *, int, int,int);
- void rapida(int *,int, int);
- int partir(int *,int, int);
- int *generarAleatorios(int N);
- int getMilisegundos(clock_t c);
- double comp=0;
- void pasar(int *,int *, int);
- //PROGRAMA PRINCIPAL
- int main()
- {
- int N,opcion1,opcion2;
- int *A =(int*)malloc(N*sizeof(double));
- clock_t t1, t2;
- int tiempo=0;
- long i;
- //PRESENTACION
- cout<<"\t\t....:::::UNIVERSIDAD NACIONAL DE TRUJILLO::::...."<<endl;
- cout<<"\t\t\t--------ESTRUCTURA DE DATOS--------"<<endl;
- cout<<"\t\t\t\tESCUELA DE INFORMATICA"<<endl<<endl;
- cout<<"\t\t\t\tALGORITMOS DE ORDENACION"<<endl<<endl;
- cout<<"\t\t\tINGRESA NUMERO DE DATOS:----->";
- cin>>N;
- A=generarAleatorios(N);
- cout<<endl<<endl<<"\t MENU 1:"<<endl;
- cout<<"\t 1_-ORDENAR ALEATORIAMENTE (Caso Normal)"<<endl;
- cout<<"\t 2_-ORDENAR ASCENDENTEMENTE (Mejor Caso)"<<endl;
- cout<<"\t 3_-ORDENAR DESCENDENTEMENTE (Peor Caso)"<<endl;
- cout<<"\t 4_-SALIR"<<endl;
- do
- {
- cout<<"\t\t-----Ingrese opcion:";
- cin>>opcion1;
- }
- while(opcion1<1||opcion1>4);
- switch(opcion1)
- {
- case 1:
- cout<<endl<<endl<<"ELEMENTOS ORDENADOS ALEATORIAMENTE:------->"<<endl;
- break;
- case 2:
- shell(N,A);
- comp=0;
- cout<<endl<<endl<<"ELEMENTOS ORDENADOS ASCENDENTEMENTE:------->"<<endl;
- break;
- case 3:
- shelldes(N,A);
- cout<<endl<<endl<<"ELEMENTOS ORDENADOS DESCENDENTEMENTE:------->"<<endl;
- break;
- case 4:
- exit(1);
- default:
- cout<<"La opcion no es correcta";
- }
- cout<<endl<<endl<<"\t MENU 2 "<<endl;
- cout<<"\t 1_-ORDENAMIENTO BURBUJA SIMPLE"<<endl;
- cout<<"\t 2_-ORDENAMIENTO BURBUJA MEJORADO"<<endl;
- cout<<"\t 3_-ORDENAMIENTO SELECCION"<<endl;
- cout<<"\t 4_-ORDENAMIENTO INSERCCION"<<endl;
- cout<<"\t 5_-ORDENAMIENTO SHELL"<<endl;
- cout<<"\t 6_-ORDENAMIENTO MEZCLA"<<endl;
- cout<<"\t 7_-ORDENAMIENTO RAPIDO"<<endl;
- cout<<"\t 8_-SALIR"<<endl;
- cout<<endl<<endl<<"\t---->Ingrese Una Opcion Del 1-8:";
- cin>>opcion2;
- switch(opcion2)
- {
- case 1:
- t1=clock();
- burbuja(N,A);
- t2=clock();
- tiempo=getMilisegundos(t2-t1);
- cout<<endl<<"[_____METODO BURBUJA SIMPLE_____]";
- cout<<endl<<"TIME :"<<tiempo;
- cout<<endl<<"COMPARACIONES :"<<comp;
- break;
- case 2:
- t1=clock();
- burbujam(N,A);
- t2=clock();
- tiempo=getMilisegundos(t2-t1);
- cout<<endl<<"[_____METODO BURBUJA MEJORADO_____]";
- cout<<endl<<"TIME :"<<tiempo;
- cout<<endl<<"COMPARACIONES :"<<comp;
- break;
- case 3:
- t1=clock();
- seleccion(N,A);
- t2=clock();
- tiempo=getMilisegundos(t2-t1);
- cout<<endl<<"[_____METODO SELECCION_____]";
- cout<<endl<<"TIME :"<<tiempo;
- cout<<endl<<"COMPARACIONES :"<<comp;
- break;
- case 4:
- t1=clock();
- inserccion(N,A);
- t2=clock();
- tiempo=getMilisegundos(t2-t1);
- cout<<endl<<"[_____METODO INSERCCION_____]";
- cout<<endl<<"TIME :"<<tiempo;
- cout<<endl<<"COMPARACIONES :"<<comp;
- break;
- case 5:
- t1=clock();
- shell(N,A);
- t2=clock();
- tiempo=getMilisegundos(t2-t1);
- cout<<endl<<"[_____METODO SHELL_____]";
- cout<<endl<<"TIME :"<<tiempo;
- cout<<endl<<"COMPARACIONES :"<<comp;
- break;
- case 6:
- t1=clock();
- merge(A,0,N-1);
- t2=clock();
- tiempo=getMilisegundos(t2-t1);
- cout<<endl<<"[_____METODO MEZCLA_____]";
- cout<<endl<<"TIME :"<<tiempo;
- cout<<endl<<"COMPARACIONES :"<<comp;
- break;
- case 7:
- t1=clock();
- rapida(A,0,N-1);
- t2=clock();
- tiempo=getMilisegundos(t2-t1);
- cout<<endl<<"[_____METODO RAPIDO_____]";
- cout<<endl<<"TIME :"<<tiempo;
- cout<<endl<<"COMPARACIONES :"<<comp;
- break;
- case 8:
- exit(1);
- default:
- cout<<"La opcion no es correcta"<<endl<<endl;
- }
- cout<<endl<<endl;
- system("PAUSE");
- return EXIT_SUCCESS;
- }
- //DESARROLLO DE LA FUNCIONES//
- //Funcion para generar los numeros aleatorios
- int *generarAleatorios(int N)
- {
- int *A=new int[N];
- srand(time(NULL));
- for(int i=0;i<N;i++)
- A[i]=(int)((((double)rand())/RAND_MAX)*MAX_LLAVE);
- return A;
- }
- //Funcion para calcular el tiempo
- int getMilisegundos(clock_t c)
- {
- int tiempo=0;
- tiempo=(int)((c/(double)CLOCKS_PER_SEC)*1000);
- return tiempo;
- }
- //METODOS DE ORDENAMIENTO
- //Metodo Burbuja Simple Ascendente
- void burbuja (int N,int *A)
- {
- comp=0;
- int B;
- for (int i=N-1;i>=1;i--)
- for(int j=0;j<=i-1;j++)
- {
- comp++;
- if (A[j]>A[j+1])
- {
- B=A[j];
- A[j]=A[j+1];
- A[j+1]=B;
- }
- }
- }
- //Metodo Burbuja Mejorada Ascendente
- void burbujam(int N,int *A)
- {
- comp=0;
- int B,i,j=1;
- int bandera=1;
- while(bandera==1)
- {
- bandera=0;
- for(i=0;i<N-j;i++)
- {
- comp++;
- if (A[i]>A[i+1])
- {
- bandera=1;
- B=A[i];
- A[i]=A[i+1];
- A[i+1]=B;
- }
- }j++;
- }
- }
- //Metodo Seleccion Ascendente
- void seleccion(int N, int *A)
- {
- comp=0;
- int B;
- for(int i=0;i<=N-2;i++)
- for(int j=i+1;j<=N-1;j++)
- {
- comp++;
- if (A[i]>A[j])
- {
- B=A[i];
- A[i]=A[j];
- A[j]=B;
- }
- }
- }
- //Metodo Inserccion Ascendente
- void inserccion(int N,int *A)
- {
- comp=0;
- int i,j;
- int temp;
- for(i=1;i<=N-1;i++)
- {
- comp++;
- temp=A[i];
- j=i-1;
- while(j>=0 && A[j]>temp)
- {
- A[j+1]=A[j];
- j--;
- }
- A[j+1]=temp;
- }
- }
- //Metodo Shell Ascendente
- void shell(int N,int *A)
- {
- comp=0;
- int i,j,bandera,B;
- for(j=N/2;j>0;j=j/2)
- {
- do
- {
- bandera=0;
- for(i=0;i<N-j;i++)
- {
- comp++;
- if(A[i]>A[i+j])
- {
- B=A[i];
- A[i]=A[i+j];
- A[i+j]=B;
- bandera=1;
- }
- }
- }
- while(bandera);
- }
- }
- //Metodo Shell Descendente
- void shelldes(int N,int *A)
- {
- int i,j,bandera,B;
- for(j=N/2;j>0;j=j/2)
- {
- do
- {
- bandera=0;
- for(i=0;i<N-j;i++)
- if(A[i]<A[i+j])
- {
- B=A[i];
- A[i]=A[i+j];
- A[i+j]=B;
- bandera=1;
- }
- }
- while(bandera);
- }
- }
- //Metodo Mezcla Ascendente
- void mezcla(int *A, int izq, int der, int p)
- {
- int tem=der-izq+1;
- int B[tem];
- int i,j,k;
- i=izq;
- j=p+1;
- k=0;
- while ((i<=p)&&(j<=der))
- {
- comp++;
- if (A[i]<A[j])
- {
- B[k]=A[i];
- i++;
- }
- else
- { B[k]=A[j];
- j++;
- }
- k++;
- }
- while(i<=p)
- {
- B[k]=A[i];
- k++;
- i++;
- }
- while(j<=der)
- {
- B[k]=A[j];
- k++;
- j++;
- }
- for(int i=0;i<=der-izq;i++)
- A[izq+i]=B[i];
- }
- void merge(int *A,int izq,int der)
- {
- int p;
- if (izq<der)
- {
- p=(izq+der)/2;
- merge(A,izq,p);
- merge(A,p+1,der);
- mezcla(A,izq,der,p);
- }
- }
- //Metodo Rapido Ascendente
- int partir(int *A,int izq, int der)
- {
- int B;
- int p=A[izq];
- int i=izq;
- int j=der-1;
- int aux=0;
- int piv=der;
- B=A[izq];
- A[izq]=A[der];
- A[der]=B;
- while(i<j)
- {
- while((A[i]<p)&&(i<j))
- i++;
- while((A[j]>p)&&(j>i))
- j--;
- comp++;
- if(i<j)
- {
- B=A[i];
- A[i]=A[j];
- A[j]=B;
- i++;
- j--;
- }
- }
- if(A[j]<=p)
- {
- B=A[piv];
- A[piv]=A[j+1];
- A[j+1]=B;
- return j+1;
- }
- else
- {
- B=A[piv];
- A[piv]=A[j];
- A[j]=B;
- return j;
- }
- }
- void rapida(int *A,int izq, int der)
- {
- int k;
- if(izq<der)
- {
- k=partir(A,izq,der);
- rapida(A,izq,k-1);
- rapida(A,k+1,der);
- }
- }

