Advertisement
35657

Untitled

Mar 16th, 2024
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<typename T>
  6. void bubble_sort(T arr[], int size) {
  7.     T temp;
  8.     for (int k = 0; k < size; k++) {
  9.         for (int j = 0; j < size - k - 1; j++) {
  10.             if (arr[j] > arr[j + 1]) {
  11.                 temp = arr[j];
  12.                 arr[j] = arr[j + 1];
  13.                 arr[j + 1] = temp;
  14.             }
  15.         }
  16.     }
  17. }
  18.  
  19. template <typename T>
  20. void display(T arr[], int size) {
  21.     for (int i = 0; i < size; i++) {
  22.         cout << arr[i] << " ";
  23.     }
  24.     cout << endl;
  25. }
  26.  
  27. int main() {
  28.  
  29.     const int size = 6;
  30.  
  31.     int arr1[6]{ 4,1,8,3,2,5 };
  32.  
  33.     double arr2[6]{ 2.3,-4.2,5.6,-3.7,8.4,-1.3 };
  34.  
  35.     display(arr1, size);
  36.     bubble_sort(arr1, size);
  37.     display(arr1, size);
  38.  
  39.     cout << endl << endl;
  40.  
  41.     display(arr2, size);
  42.     bubble_sort(arr2, size);
  43.     display(arr2, size);
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement