Simple Bubble sort C++ function

JLChafardet 0 Tallied Votes 205 Views Share

This code is an example of a simple bubble sort function in C++.

the function doesnt receive or returns nothing

just for the record, some translations

"declaracion de constante" = constant declaration
"prototipo de funcion" = function prototype

"arreglo" = array
"Arreglo en el orden original" = original array order
"temporal" = temp

"Ordenado de mayor a menor" = Ascending order (from 0 to N [where n can be any number larger than 0])
"Ordenado de menor a mayor" = Descending order (from N to 0 [where n can be any number largern than 0])

hope someone find it useful.

JLChafardet

#include <cstdlib>
#include <iostream>

using namespace std;

// declaracion de constante
const int FIL = 10;
// prototipo de funcion
int bubblesort();

int main(int argc, char *argv[])
{
    system("TITLE Bubble sort - Desarrollado por: José Luis Chafardet Grimaldi");
    system("COLOR 1F");
	bubblesort();
    system("PAUSE");
    return EXIT_SUCCESS;
}

// funcion bubblesort
int bubblesort()
{
    int arreglo[ FIL ] = {31,17,21,5,54,88,22,19,46,10};
	int a, b, temporal;
    cout << "Arreglo en el orden original: " << endl;
    for(a=0;a<10;a++) cout << arreglo[ a ] << " ";
    cout << endl;
    for (a=9;a>=0;a--)
    {
    for(b=0;b<a;b++)
    {
         if(arreglo[ b ] < arreglo[ b+1 ])
         {
             temporal = arreglo[ b+1 ];
             arreglo[ b+1 ] = arreglo[ b ];
             arreglo[ b ] = temporal;
             }
         }
         }
    cout << endl << "Ordenado de mayor a menor: " << endl;
    for(a=0;a<10;a++) cout << "[ " << arreglo[ a ] << " ]";
    cout << endl;
    
        for(a=0;a<10;a++);
    cout << endl;
    for (a=9;a>=0;a--)
    {
    for(b=0;b<a;b++)
    {
         if(arreglo[ b ] > arreglo[ b+1 ])
         {
             temporal = arreglo[ b+1 ];
             arreglo[ b+1 ] = arreglo[ b ];
             arreglo[ b ] = temporal;
             }
         }
         }
    cout << "Ordenado de menor a mayor: " << endl;
    for(a=0;a<10;a++) cout << "[ " << arreglo[ a ] << " ]";
    cout << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.