Ola este es el Laboratorio 04 del curso de estructuras de datos que trata sobre metodos de ordenacion y búsqueda.. espero les ayude.. Saludos
01. Métodos de Ordenación:
/* Metodos de Ordenacion */
#include<iostream>
using namespace std;
//////////////////// INSERTAR ELEMENTO EN EL ARREGLO ///////////////////////////
int *Insertar(int *A ,int N)
{
for( int i = 0 ; i < N ; i++ )
{
cout<<" - Ingrese Elemento ["<<i + 1<<"] = ";
cin>>A[i];
}
return A;
}
///////////////////// MOSTRAR ELEMENTO DEL ARREGLO ////////////////////////////
void Mostrar(int *A ,int N)
{
for( int i = 0 ; i < N ; i++ )
{ cout<<" "<<A[i];}
}
///////////////////////// ORDENACION POR BURBUJA //////////////////////////////
int *Burbuja(int *A , int N)
{
int aux;
for( int i = N - 1 ; i > 0 ; i-- )
for( int j = 0 ; j < i ; j++ )
if ( A[j] > A[j + 1])
{
aux = A[j];
A[j] = A[j+1];
A[j+1] = aux;
}
return A;
}
////////////////////////// ORDENACION POR INSERCION ////////////////////////////
int *Insercion(int *A , int N)
{
int aux;
int i,j;
for ( i = 1 ; i < N ; i++ )
{
aux = A[i];
j = i - 1;
while( j >= 0 && A[j] > aux )
{
A[j+1] = A[j];
j--;
}
A[j+1] = aux;
}
return A;
}
///////////////////////// ORDENACION POR SELECCION /////////////////////////////
int *Seleccion(int *A , int N)
{
int minimo;
int aux;
for ( int i = 0 ; i < N ; i++ )
{
minimo = i;
for ( int j=i+1 ;j < N ; j++ )
if ( A[j] < A[minimo] )
minimo = j;
aux = A[i];
A[i] = A[minimo];
A[minimo] = aux;
}
return A;
}
//////////////////////////// ORDENACION POR SHELL //////////////////////////////
int *Shell(int *A , int N)
{
int i,j,bandera,aux;
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] )
{
aux = A[i];
A[i] = A[i+j];
A[i+j] = aux;
bandera=1;
}
}
}
while(bandera);
}
return A;
}
//////////////// ORDENACION POR QUICKSORT (Recursiva)///////////////////////////
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--;
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;
}
}
int *Rapida(int *A , int der , int izq )
{
int k;
if( izq < der )
{
k = partir( A , izq , der );
Rapida( A , izq , k-1 );
Rapida( A , k+1 , der);
}
return A;
}
/////////////////// ORDENACION POR MERGESORT (Recursiva) ///////////////////////
void Intercalar(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))
{
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];
}
/////////////////////////////////////////////////////
int *Mezcla(int *A , int izq , int der )
{
int p;
if ( izq < der )
{
p = ( izq + der ) / 2;
Mezcla( A , izq , p );
Mezcla( A , p + 1 , der );
Intercalar( A , izq , der , p );
}
return A;
}
/////////////////////////////////// MAIN /////////////////////////////////////
int main()
{
system("COLOR 5F");
int *A = NULL;
A = new int;
int N;
int opcion;
char R = 'S';
do
{
cout<<endl<<"\t ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» ";
cout<<endl<<"\t º |<-[ UNIVERSIDAD NACIONAL DE TRUJILLO ]->| º ";
cout<<endl<<"\t º CURSO ::: ESTRUCTURA DE DATOS º ";
cout<<endl<<"\t º Laboratio 04 - ALGORITMOS DE ORDENACION º ";
cout<<endl<<"\t º Hecho por InfoBik's ---> JP. Rodriguez º ";
cout<<endl<<"\t ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ "<<endl;
cout<<endl<<" >> INGRESE CANTIDAD DE ELEMENTOS : ";
cin>>N;
cout<<endl;
A = Insertar( A , N );
cout<<endl<<endl;
cout<<"\t\t ELIJA UN METODO DE ORDENACION "<<endl;
cout<<"\t\t ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ "<<endl;
cout<<"\t\t ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»"<<endl;
cout<<"\t\t º ITERATIVOS º"<<endl;
cout<<"\t\t º ------------ º"<<endl;
cout<<"\t\t º 1. Burbuja º"<<endl;
cout<<"\t\t º 2. Insercion º"<<endl;
cout<<"\t\t º 3. Seleccion º"<<endl;
cout<<"\t\t º 4. Shell º"<<endl;
cout<<"\t\t ºÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͺ"<<endl;
cout<<"\t\t º RECURSIVOS º"<<endl;
cout<<"\t\t º ------------ º"<<endl;
cout<<"\t\t º 5. QuickSort º"<<endl;
cout<<"\t\t º 6. MergeSort º"<<endl;
cout<<"\t\t ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ "<<endl;
do
{
cout<<endl<<endl<<" >> INGRESE OPCION: ";
cin>>opcion;
}
while(opcion < 1 || opcion > 6);
{
cout<<endl;
switch ( opcion )
{
case 1:
A = Burbuja( A , N );
cout<<endl<<" EL ARREGLO MEDIANTE BURBUJA QUEDA ASI: ";
Mostrar(A , N);
break;
case 2:
A = Insercion( A , N );
cout<<endl<<" EL ARREGLO MEDIANTE INSERCION QUEDA ASI: ";
Mostrar(A , N);
break;
case 3:
A = Seleccion( A , N );
cout<<endl<<" EL ARREGLO MEDIANTE SELECCION QUEDA ASI: ";
Mostrar(A , N);
break;
case 4:
A = Shell( A , N );
cout<<endl<<" EL ARREGLO MEDIANTE SHELL QUEDA ASI: ";
Mostrar(A , N);
break;
case 5:
A = Rapida( A , 0 , N - 1 );
cout<<endl<<" EL ARREGLO MEDIANTE QUICKSORT QUEDA ASI: ";
Mostrar(A , N);
break;
case 6:
A = Mezcla( A , 0 , N - 1 );
cout<<endl<<" EL ARREGLO MEDIANTE MERGESORT QUEDA ASI: ";
Mostrar(A , N);
break;
default:
cout<<" LA OPCION INGRESADA NO ES VALIDA ";
}
}
cout<<"\n\n\ DESEA CONTINUAR [S/N] : ";
cin>>R;
system("CLS");
}
while ( R == 'S' || R == 's' );
}
02. Metodos de Busqueda:
/* Metodos de Busqueda */
#include<iostream>
#include<cstdlib>
using namespace std;
int buscar(int A[],int n,int v) ////////////
{
pos=-1;
encontrado=0;
i=0;
while((i<n)&&(!encontrado))
{
if(A[]==valorencontrado)
{
pos=i;
encontrado=1;
}
i=i+1;
}
return(pos);
}
main()
{
system("color 5F");
int n,A[50],i;
cout<<endl<<"\t ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» ";
cout<<endl<<"\t º |<-[ UNIVERSIDAD NACIONAL DE TRUJILLO ]->| º ";
cout<<endl<<"\t º CURSO ::: ESTRUCTURA DE DATOS º ";
cout<<endl<<"\t º Laboratio 04 - ALGORITMOS DE BUSQUEDA º ";
cout<<endl<<"\t º Hecho por InfoBik's ---> JP. Rodriguez º ";
cout<<endl<<"\t ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ "<<endl;
cout<<endl<<" >> INGRESE CANTIDAD DE ELEMENTOS: ";
cin>>n;
for(i=0; i<n ;i++) ///////// Insertar Elementos en el Arreglo
{
cout<<" Ingrese Elemento ["<<i+1<<"] : ";
cin>>A[i];
}
cout<<endl<<" Los Elementos Ingresados son: "; ///// Mostrar Elementos del Arreglo
for(i=0; i<n ;i++)
{
cout<<A[i]<<" ";
}
cout<<endl<<endl;
system("pause");
}
No hay comentarios.:
Publicar un comentario