Ordenamiento por Burbuja C++

Roberto
25 de Febrero del 2006
Hola! quisiera saber si alguien tiene el programa de ordenamiento por el metodo de la burbuja que le corra perfectamente para una cantidad n de numeros, yo lo hice pero si le trato de meter mas de 6 numeros, ya no me los ordena correctamente, agradeceria si alguien me enviara el codigo del programa funcionando, por cualquier cosa mi correo es [email protected] , gracias!

Noel Solw
25 de Febrero del 2006
// program LR_6.cpp -pag 306
// bubbles sort - from left to right.
// 9/10/2003
// writen in Borlandc CPP - ver 3.1

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

#define MAX 20

void Initialize(int *a)
{
randomize();
for(int i = 0; i < MAX;i++)
a[i] = random(100);
} // INITIALIZE

void Print(int *a,char *msg)
{
cout << setw(15) << msg << " : ";
for(int i = 0;i < MAX-1;i++)
cout << a[i] << ",";
cout << a[i] << endl;
} // PRINT

void swap(int &a,int &b)
{
int c = a;
a = b;
b = c;
} // SWAP

void BubblesSort(int *a)
{
int sw;
for(int k = 0;k < MAX;k++)
{
sw = 0;
for(int i = MAX-1;i > k;i--)
if(a[i-1] > a[i])
{
swap(a[i],a[i-1]);
sw = 1;
}
if(!sw)
break;
Print(a," ");
}
} // BUBBLES SORT

void Process()
{
int a[MAX];
Initialize(a);
Print(a,"source array");
cout << endl;
BubblesSort(a);
cout << endl;
Print(a,"sorted array");
} // PROCESS

void main()
{
clrscr();
cout << "bubbles sort - from left to right : n";
cout << "-----------------------------------------------------"
"-------------------------n";
Process();
cout << "-----------------------------------------------------"
"-------------------------n";
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN

/*
bubbles sort - from left to right :
------------------------------------------------------------------------------
source array : 54,16,9,66,7,84,39,2,31,17,73,81,97,92,95,51,41,15,67,5

: 2,54,16,9,66,7,84,39,5,31,17,73,81,97,92,95,51,41,15,67
: 2,5,54,16,9,66,7,84,39,15,31,17,73,81,97,92,95,51,41,67
: 2,5,7,54,16,9,66,15,84,39,17,31,41,73,81,97,92,95,51,67
: 2,5,7,9,54,16,15,66,17,84,39,31,41,51,73,81,97,92,95,67
: 2,5,7,9,15,54,16,17,66,31,84,39,41,51,67,73,81,97,92,95
: 2,5,7,9,15,16,54,17,31,66,39,84,41,51,67,73,81,92,97,95
: 2,5,7,9,15,16,17,54,31,39,66,41,84,51,67,73,81,92,95,97
: 2,5,7,9,15,16,17,31,54,39,41,66,51,84,67,73,81,92,95,97
: 2,5,7,9,15,16,17,31,39,54,41,51,66,67,84,73,81,92,95,97
: 2,5,7,9,15,16,17,31,39,41,54,51,66,67,73,84,81,92,95,97
: 2,5,7,9,15,16,17,31,39,41,51,54,66,67,73,81,84,92,95,97

sorted array : 2,5,7,9,15,16,17,31,39,41,51,54,66,67,73,81,84,92,95,97
------------------------------------------------------------------------------
end of program - good bye ! ! !
*/