35657

Untitled

Dec 16th, 2023
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. void insert_sort(T arr[], int size) {
  7.     for (int i = 1; i < size; i++) {
  8.         T x = arr[i];
  9.         int k = i - 1;
  10.         while (k >= 0 && arr[k] > x) {
  11.             arr[k + 1] = arr[k];
  12.             arr[k] = x;
  13.             k--;
  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.     int arr[]{ 1, 3, 7, -4, -2, 4 };
  30.     int size = 6;
  31.     cout << "Original int Array : ";
  32.     display(arr, size);
  33.     insert_sort(arr, size);
  34.     cout << "Sorted   int Array : ";
  35.     display(arr, size);
  36.  
  37.  
  38.     // будет ли работать с double?
  39.      double doubleArray[]{ 3.5, 2.5, 3.7, 1.0, 3.3 };
  40.      size = 5;
  41.      cout << "Original double Array : ";
  42.      display(doubleArray, size);
  43.      insert_sort(doubleArray, size);
  44.      cout << "Sorted double Array : ";
  45.      display(doubleArray, size);
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment