35657

Untitled

Mar 12th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. template <typename T>
  7. void sort(T array[], size_t size) {
  8.     for (int k = 0; k < size; k++) {
  9.         for (int j = 0; j < size - 1; j++) {
  10.             if (array[j] > array[j + 1]) {
  11.                 T temp = array[j];
  12.                 array[j] = array[j + 1];
  13.                 array[j + 1] = temp;
  14.             }
  15.         }
  16.     }
  17. }
  18.  
  19.  
  20.  
  21. template<typename T>
  22. void display(T array[], size_t size) {
  23.     for (int i = 0; i < size; i++) {
  24.         cout << array[i] << " ";
  25.     }
  26.     cout << endl;
  27. }
  28.  
  29.  
  30. int main()
  31. {
  32.     cout << "Sort Template" << endl << endl;
  33.  
  34.     int intArray[]{ 1, 3, 7, -4, -2, 4 };
  35.     int size = sizeof(intArray) / sizeof(int);
  36.     cout << "Original int Array : ";
  37.     display(intArray, size);
  38.     sort(intArray, size);
  39.     cout << "Sorted   int Array : ";
  40.     display(intArray, size);
  41.  
  42.     char chrArray[]{ 'o', 't', 't', 'f', 'f', 's', 's', 'e', 'n' };
  43.     size = sizeof(chrArray) / sizeof(char);
  44.     cout << "Original chr Array : ";
  45.     display(chrArray, size);
  46.     sort(chrArray, size);
  47.     cout << "Sorted   chr Array : ";
  48.     display(chrArray, size);
  49.  
  50.     string strArray[]{ "one", "two", "three", "four", "five" };
  51.     size = sizeof(strArray) / sizeof(string);
  52.     cout << "Original str Array : ";
  53.     display(strArray, size);
  54.     sort(strArray, size);
  55.     cout << "Sorted   str Array : ";
  56.     display(strArray, size);
  57. }
  58.  
Add Comment
Please, Sign In to add comment