Programacion c++ – Método de ordenamiento burbuja


Arreglos en c++ ejercicios resueltos

Método de ordenamiento burbuja

Hacer un programa ingresando n limite de arreglo, el arreglo deberá estar ordenado por el método de la burbuja.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#define MAX 50
struct ordenamiento
{int elem ;
};

void leerarray(int,struct ordenamiento []);
void burbuja (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);
burbuja (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 burbuja (int n,struct ordenamiento a[])
{
int i,j;
struct ordenamiento temp;
for(i=1;i<n;i++)
for(j=n-1;j>=i;j--)
if(a[j-1].elem >a[j].elem)
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
void muestra (int n,struct ordenamiento a[])
{
for(int i=0;i<n;i++)
cout<<" "<<a[i].elem;
}

Te comparto el siguiente ejercicio, donde se aplica el Método burbuja en un arreglo unidimensional:Método burbuja – Arreglos

Para descargar el ejercicio:

-->