Advertisement
35657

Untitled

Jul 22nd, 2023
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. void bubble_sort(T array[], int size) {
  7.     for (int k = 0; k < size; k++) {
  8.         for (int j = 0; j < size - 1; j++) {
  9.             if (array[j] > array[j + 1]) {
  10.                 T temp = array[j];
  11.                 array[j] = array[j + 1];
  12.                 array[j + 1] = temp;
  13.             }
  14.         }
  15.     }
  16. }
  17.  
  18.  
  19. template <typename T>
  20. void display(T array[], int size) {
  21.     for (int i = 0; i < size; i++) {
  22.         cout << array[i] << " ";
  23.     }
  24.     cout << endl;
  25. }
  26.  
  27.  
  28. int main() {
  29.     setlocale(LC_ALL, "ru");
  30.  
  31.     int array[]{ 1, 3, 7, -4, -2, 4 };
  32.     int size = 6;
  33.     cout << "Original int Array : ";
  34.     display(array, size);
  35.     bubble_sort(array, size);
  36.     cout << "Sorted   int Array : ";
  37.     display(array, size);
  38.  
  39.  
  40.     double doublearray[]{ 3.5, 2.5, 3.7, 1.0, 3.3 };
  41.     size = 5;
  42.     cout << "Original double Array : ";
  43.     display(doublearray, size);
  44.     bubble_sort(doublearray, size);
  45.     cout << "Sorted double Array : ";
  46.     display(doublearray, size);
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement