Utilizamos cookies propias y de terceros para mejorar la experiencia de navegación, y ofrecer contenidos y publicidad de interés. Al continuar con la navegación entendemos que se acepta nuestra política de cookies.
main(){
int tmp,iaux;
int array[N];
//Inicializacion del arreglo, con los 5 elementos
cout<<"INGRESE 5 NUMEROS QUE DESEE ORDENAR "<<"\n\n";
for(int i=0;i<N;i++){
cout<<"INGRESE EL NUMERO EN LA POSICION "<<i<<": ";
cin>>array[i];
}
cout<<"\nNUMEROS ORDENADOS ASCENDENTEMENTE"<<"\n";
//BubbleSort que ordena los numeros ascendentemente.
for(int i=0;i<N;i++){
for(int j=0;j<N-1;j++){
if (array[j] > array[j + 1])
{
iaux = array[j];
array[j] = array[j + 1];
array[j + 1] = iaux;
}
}
}
//Despliega el arreglo que ordena los numeros ascendentemente
for(int i=0;i<N;i++){
cout<<array[i]<<"\n";
}
cout<<"\n\nNUMEROS ORDENADOS DESCENDENTEMENTE"<<"\n";
//BubbleSort que ordena los numeros descendentemente
for(int i=0;i<N;i++){
for(int j=0;j<N-1;j++){
if (array[j] < array[j + 1])
{
iaux = array[j];
array[j] = array[j + 1];
array[j + 1] = iaux;
}
}
}
/*Puede sustituirlo por la funcion por
if(array[j]>array[j+1]){
swap(array[j], array[j+1]); // funcion que hace el intercambio
*/
//Despliega el arreglo que ordena los numeros descendentemente
for(int i=0;i<N;i++){
cout<<array[i]<<"\n";
}
getch();
}
Comentarios sobre la versión: Versión 1 (3)
#include <stdlib.h>
#include <conio.h>
#include <string>
#define N 5 //Define una constante con N=5
using namespace std;
main(){
int tmp,iaux;
int array[N];
//Inicializacion del arreglo, con los 5 elementos
cout<<"INGRESE 5 NUMEROS QUE DESEE ORDENAR "<<"\n\n";
for(int i=0;i<N;i++){
cout<<"INGRESE EL NUMERO EN LA POSICION "<<i<<": ";
cin>>array[i];
}
cout<<"\nNUMEROS ORDENADOS ASCENDENTEMENTE"<<"\n";
//BubbleSort que ordena los numeros ascendentemente.
for(int i=0;i<N;i++){
for(int j=0;j<N-1;j++){
if (array[j] > array[j + 1])
{
iaux = array[j];
array[j] = array[j + 1];
array[j + 1] = iaux;
}
}
}
//Despliega el arreglo que ordena los numeros ascendentemente
for(int i=0;i<N;i++){
cout<<array[i]<<"\n";
}
cout<<"\n\nNUMEROS ORDENADOS DESCENDENTEMENTE"<<"\n";
//BubbleSort que ordena los numeros descendentemente
for(int i=0;i<N;i++){
for(int j=0;j<N-1;j++){
if (array[j] < array[j + 1])
{
iaux = array[j];
array[j] = array[j + 1];
array[j + 1] = iaux;
}
}
}
/*Puede sustituirlo por la funcion por
if(array[j]>array[j+1]){
swap(array[j], array[j+1]); // funcion que hace el intercambio
*/
//Despliega el arreglo que ordena los numeros descendentemente
for(int i=0;i<N;i++){
cout<<array[i]<<"\n";
}
getch();
}