martes, 9 de noviembre de 2010

Metodos de ordenamiento

PROGRAMA QUE CAPTURA VALORES NUMERICOS EN UNA MATRIZ DE 3X3 Y LOS ORDENA DE MANERA ASCENDENTE.MOSTRANDO LA MATRIZ DESORDENADA Y ORDENADA.

#include<conio.h>
#include<iostream.h>

void main()
{
clrscr();
int i,j,k,l;
int a[3][3];
int aux;
cout<<"Introduze tus valores:\n"<<endl;
for(i=0;i<3;i++){
   for(j=0;j<3;j++){
      cin>>a[i][j];
}
}
cout <<"\nLos valores desordenados son: \n "<<endl;
for(i=0;i<3;i++){
   for(j=0;j<3;j++){

cout<<a[i][j]<<" ";//impresion de la matriz desordenada
}
cout<<"  \n "<<endl;
}

for(i=0;i<3;i++){//ordenamiento de la matriz por metodo de burbuja
      for(j=0;j<3;j++){    
if(a[i][j]>a[i][j+1])
{
aux=a[i][j];
  a[i][j]=a[i][j+1];
    a[i][j+1]=aux;
for(k=0;k<3;k++){
           for(l=0;l<3;l++){
if(a[k][l+2]>a[k+1][l])
{
aux=a[k][l+2];
  a[k][l+2]=a[k+1][l];
    a[k+1][l]=aux;     
}
}
}
}
}
}

cout <<"Los valores ordenados de manera ascendente son: \n "<<endl;
  for(i=0;i<3;i++){
    for(j=0;j<3;j++){
     cout<<a[i][j]<<" ";
}
cout<<" \n "<<endl;
}
getch();
}

No hay comentarios:

Publicar un comentario