Jump to content

[C++] bubbleSort (crescator/descrescator)


Andreigl
 Share

Recommended Posts

#include <iostream>

void bubbleSort(int Array[], int ArraySize, void (*calledFunc)(int [], int)) {
    return (*calledFunc)(Array, ArraySize);
}

void swap(int *Numar_1, int *Numar_2, bool Case) {
    if (Case) {
        int CopyNr_1 = *Numar_1;
        *Numar_1 = *Numar_2;
        *Numar_2 = CopyNr_1;
    } else {
        int CopyNr_2 = *Numar_2;
        *Numar_2 = *Numar_1;
        *Numar_1 = CopyNr_2;
    }
}

void Crescator(int Array[], int ArraySize) {
    for (int i=0; i<ArraySize-1; i++) {
        for (int j=0; j<ArraySize-i-1; j++) {
            if (Array[j] > Array[j+1]) {
                swap(&Array[j], &Array[j+1], true);
            }
        }
    }
}

void Descrescator(int Array[], int ArraySize) {
    for (int i=0; i<ArraySize-1; i++) {
        for (int j=0; j<ArraySize-i-1; j++) {
            if (Array[j] < Array[j+1]) {
                swap(&Array[j], &Array[j+1], false);
            }
        }
    }
}

void printArray(int Array[], int ArraySize) {
    for (int i=0; i<ArraySize; i++)
        std::cout << Array[i] << " ";
}

int main() {
    int Numar, ArraySize=0;
    int Array[100];

    do {
        std::cin >> Numar;

        if (Numar != 0) {
            Array[ArraySize] = Numar;
            ArraySize++;
        }
    } while (Numar != 0);

    std::cout << "Crescator: ";
    (*bubbleSort)(Array, ArraySize, Crescator);
    printArray(Array, ArraySize);

    std::cout << std::endl << "Descrescator: ";
    (*bubbleSort)(Array, ArraySize, Descrescator);
    printArray(Array, ArraySize);

}

 

Edited by shanker'
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.