1.- Realizar un programa que permita ingresar n cantidad de elementos de un arreglo y que nos muestre los elementos ordenados mediante el uso del método de inserción.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define MAX 50
struct ordenamiento
{int elem ;
};
void leerarray(int,struct ordenamiento []);
void insercion (int,struct ordenamiento []);
void muestra (int,struct ordenamiento []);
void main()
{
int n;
struct ordenamiento orden[MAX];
cout<<"Ingrese limite del arreglo:";
cin>>n;
leerarray(n,orden);
insercion (n,orden);
cout<<endl<<"El arreglo ordenado es:"<<endl;
muestra(n,orden);
getch();
}
void leerarray(int n,struct ordenamiento a[])
{ for(int i=0;i<n;i++)
{
cout<<"Ingrese elemento "<<i<<":";
cin>>a[i].elem;
}
}
void insercion (int n,struct ordenamiento a[])
{
int i,j;
struct ordenamiento temp;
for(i=1;i<n;i++)
{ temp=a[i];
for(j=i-1;j>=0 && temp.elem<a[j].elem ;j--)
a[j+1]=a[j];
a[j+1]=temp;
}
}
void muestra (int n,struct ordenamiento a[])
{
for(int i=0;i<n;i++)
cout<<" "<<a[i].elem;
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define MAX 50
struct ordenamiento
{int elem ;
};
void leerarray(int,struct ordenamiento []);
void insercion (int,struct ordenamiento []);
void muestra (int,struct ordenamiento []);
void main()
{
int n;
struct ordenamiento orden[MAX];
cout<<"Ingrese limite del arreglo:";
cin>>n;
leerarray(n,orden);
insercion (n,orden);
cout<<endl<<"El arreglo ordenado es:"<<endl;
muestra(n,orden);
getch();
}
void leerarray(int n,struct ordenamiento a[])
{ for(int i=0;i<n;i++)
{
cout<<"Ingrese elemento "<<i<<":";
cin>>a[i].elem;
}
}
void insercion (int n,struct ordenamiento a[])
{
int i,j;
struct ordenamiento temp;
for(i=1;i<n;i++)
{ temp=a[i];
for(j=i-1;j>=0 && temp.elem<a[j].elem ;j--)
a[j+1]=a[j];
a[j+1]=temp;
}
}
void muestra (int n,struct ordenamiento a[])
{
for(int i=0;i<n;i++)
cout<<" "<<a[i].elem;
}
Comentarios
Publicar un comentario