Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Name: Bubble Sort Function Ascendingly/Descendingly
- // Author: Stf Kolew
- // Arguments: Array(int, double, char, custom type), array size(size_t - int), bool
- //
- // Sample usage:
- //
- // Descending order:
- // bubble_sort_array<int>(array, array_size, false);
- //
- // Ascending Order:
- // bubble_sort_array<int>(array, array_size, true);
- //
- // Greetings
- template <class T>
- T bubble_sort_array(T array[], size_t array_size, bool type = false) {
- for (size_t i = 0; i < array_size - 1; i++) {
- for(size_t j = i + 1; j < array_size; j++)
- if( (type ? (array[i] > array[j]) : (array[i] < array[j]))) {
- T temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- return *array;
- }
Add Comment
Please, Sign In to add comment