Skip to content

Instantly share code, notes, and snippets.

@SamoraMachel
Created June 3, 2021 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SamoraMachel/3edd1eb5a53cc0fb85353ba98a70d6bf to your computer and use it in GitHub Desktop.
Save SamoraMachel/3edd1eb5a53cc0fb85353ba98a70d6bf to your computer and use it in GitHub Desktop.
# 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