Garey

Bubble Sort Function

Dec 19th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. // Name: Bubble Sort Function Ascendingly/Descendingly
  2. // Author: Stf Kolew
  3. // Arguments: Array(int, double, char, custom type), array size(size_t - int), bool
  4. //
  5. // Sample usage:
  6. //
  7. // Descending order:
  8. // bubble_sort_array<int>(array, array_size, false);
  9. //
  10. // Ascending Order:
  11. // bubble_sort_array<int>(array, array_size, true);
  12. //
  13. // Greetings
  14.  
  15. template <class T>
  16. T bubble_sort_array(T array[], size_t array_size, bool type = false) {
  17.     for (size_t i = 0; i < array_size - 1; i++) {
  18.         for(size_t j = i + 1; j < array_size; j++)
  19.             if( (type ? (array[i] > array[j]) : (array[i] < array[j]))) {
  20.                 T temp = array[i];
  21.                 array[i] = array[j];
  22.                 array[j] = temp;
  23.             }
  24.     }
  25.  
  26.     return *array;
  27. }
Add Comment
Please, Sign In to add comment