Created
June 3, 2021 15:02
-
-
Save SamoraMachel/3edd1eb5a53cc0fb85353ba98a70d6bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <iostream> | |
# include <algorithm> | |
using namespace std; | |
template <class T> | |
bool ascending(T a, T b){ | |
return a > b; | |
} | |
template <class T> | |
bool descending (T a, T b){ | |
return a < b; | |
} | |
template <class T> | |
void display(T *array, const int size){ | |
for (int iterator = 0; iterator < size; iterator++){ | |
cout << array[iterator] << " "; | |
} | |
cout << "\n\n"; | |
} | |
template <class T> | |
void bubbleSort(T *const array, const int size, bool (*order)(T, T) = ascending){ | |
bool swapped = 0; | |
for (int iterator = 0; iterator < size-1 ; iterator++){ | |
for (int iter = 0; iter < size ; iter++){ | |
swapped = 0; | |
if (order(array[iter], array[iter+1])){ | |
swap(array[iter], array[iter+1]); | |
swapped = 1; | |
} | |
} | |
display(array, size); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment