C++ Bubble sort Program

//Bubble sort

#include 
using namespace std;

int compare(int, int);
void sort(int[], const int);
void swap(int *, int *);

int compare(int x, int y) {
    return(x > y);
}

void swap(int *x, int *y) {
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void display(int array[], int n) {
    for (int i=0; i<n; i++) {
        cout << array[i] << " ";
    }
    cout << endl;
}

void sort(int table[], const int n) {
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n-1; j++) {
            if(compare(table[j], table[j+1]))
                swap(&table[j], &table[j+1]);
        }
        cout << "Pass " << i+1 << ": ";
        display(table, n);
    }
}

int quantity;
int* tab;

int main() {
    cout <> quantity;

    tab = new int [quantity];
    cout << "Input numbers: \n\n";

    for (int i = 0; i < quantity; i++) {
        int x = i;
        cout << "#" << ++x <> tab[i];
    }

    cout << "\nBefore sorting: ";
    display(tab, quantity);
    cout << endl;

    sort(tab, quantity);

    cout << "\nAfter sorting: ";
    display(tab, quantity);
    cout << endl;

    return 0;
}

Advertisements