Ordenamiento burbuja en c++

Kathis64 · August 10, 2023
#include <iostream>

using namespace std;

void OrdenarBurbuja(int arreglo[], int tamano) {
    for(int i = 0; i < tamano - 1; i++){
        for(int j = 0; j < tamano - i - 1; j++){
            if(arreglo[j] < arreglo[j + 1]){
                swap(arreglo[j], arreglo[j + 1]);
            }
        }
    }
}

int main(){
    int tamanoArreglo;

    cout << "Ingresa la cantidad de números (máximo 10): ";
    cin >> tamanoArreglo;

    if (tamanoArreglo <= 10){
        int numeros[tamanoArreglo];

        for (int i = 0; i < tamanoArreglo; i++){
            cout << "Ingresa el número " << i + 1 << ": ";
            cin >> numeros[i];
        }

        OrdenarBurbuja(numeros, tamanoArreglo);

        cout << "Números ordenados de mayor a menor:" << endl;
        for(int i = 0; i < tamanoArreglo; i++){
            cout << numeros[i] << " ";
        }
        cout << endl;
    } else{
        cout << "La cantidad ingresada es mayor a 10. Inténtalo de nuevo." << endl;
    }

    return 0;
}
Output

Comments

Please sign up or log in to contribute to the discussion.