Advertisement
35657

Untitled

Dec 14th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. void sort(T array[], int size) {
  7. for (int k = 0; k < size; k++) {
  8. for (int j = 0; j < size - k - 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. template <typename T>
  19. void display(T array[], int size) {
  20. for (int i = 0; i < size; i++) {
  21. cout << array[i] << " ";
  22. }
  23. cout << endl;
  24. }
  25.  
  26.  
  27. int main() {
  28. int array[]{ 1, 3, 7, -4, -2, 4 };
  29. int size = 6;
  30. cout << "Original int Array : ";
  31. display(array, size);
  32. sort(array, size);
  33. cout << "Sorted int Array : ";
  34. display(array, size);
  35.  
  36.  
  37. double doublearray[]{ 3.5, 2.5, 3.7, 1.0, 3.3 };
  38. size = 5;
  39. cout << "Original double Array : ";
  40. display(doublearray, size);
  41. sort(doublearray, size);
  42. cout << "Sorted double Array : ";
  43. display(doublearray, size);
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement