Advertisement
35657

Untitled

Mar 16th, 2024
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 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.     bool b = true; // произошла ли замена при проходе по массиву
  8.     T temp;
  9.     int k = 0;
  10.     while(b) {
  11.         b = false;
  12.         for (int j = 0; j < size - k - 1; j++) {
  13.             if (arr[j] > arr[j + 1]) {
  14.                 temp = arr[j];
  15.                 arr[j] = arr[j + 1];
  16.                 arr[j + 1] = temp;
  17.                 b = true;
  18.             }
  19.         }
  20.         k++;
  21.     }
  22. }
  23.  
  24. template <typename T>
  25. void display(T arr[], int size) {
  26.     for (int i = 0; i < size; i++) {
  27.         cout << arr[i] << " ";
  28.     }
  29.     cout << endl;
  30. }
  31.  
  32. int main() {
  33.  
  34.     const int size = 6;
  35.  
  36.     int arr1[6]{ 4,1,8,3,2,5 };
  37.  
  38.     double arr2[6]{ 2.3,-4.2,5.6,-3.7,8.4,-1.3 };
  39.  
  40.     display(arr1, size);
  41.     bubble_sort(arr1, size);
  42.     display(arr1, size);
  43.  
  44.     cout << endl << endl;
  45.  
  46.     display(arr2, size);
  47.     bubble_sort(arr2, size);
  48.     display(arr2, size);
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement