Advertisement
Guest User

Untitled

a guest
May 26th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #ifndef Array_H
  2. #define Array_H
  3.  
  4. #include <iostream>
  5.  
  6. template <class T>
  7. class Array
  8. {
  9. private:
  10. int size;
  11. T *myArray;
  12. public:
  13. Array();
  14. Array(int);
  15. void setArray(int, T);
  16. void getArray();
  17. ~Array();
  18.  
  19. void bubbleSort(int);
  20.  
  21. };
  22.  
  23. template <class T>
  24. Array<T>::Array()
  25. {
  26. size = 0;
  27. }
  28.  
  29. template <class T>
  30. Array<T>::Array(int s)
  31. {
  32. size = s;
  33. myArray = new T[size];
  34. }
  35.  
  36. template <class T>
  37. void Array<T>::setArray(int elem, T val)
  38. {
  39. myArray[elem] = val;
  40. }
  41.  
  42. template <class T>
  43. void Array<T>::getArray()
  44. {
  45. for (int j = 0; j < size; j++)
  46. {
  47. std::cout << myArray[j] << ' ';
  48. }
  49. std::cout << '\n';
  50. }
  51.  
  52. template <class T>
  53. Array<T>::~Array()
  54. {
  55. delete myArray;
  56. }
  57.  
  58. template <class T>
  59. void Array<T>::bubbleSort(int n)
  60. {
  61. T temp;
  62.  
  63. if (n == 1)
  64. return;
  65. for (int i = 0; i < n - 1; i++)
  66. {
  67. if (myArray[i] > myArray[i+1])
  68. {
  69. temp = myArray[i];
  70. myArray[i] = myArray[i+1];
  71. myArray[i+1] = temp;
  72. }
  73. }
  74.  
  75. std::cout << "Sorting Array: ";
  76. for (int j = 0; j < size; j++)
  77. {
  78. std::cout << myArray[j] << ' ';
  79. }
  80. std::cout << '\n';
  81.  
  82. bubbleSort(n - 1);
  83. }
  84.  
  85. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement