Advertisement
Guest User

Untitled

a guest
May 31st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. //ALG ARRAY HEADER
  2. #ifndef _ALGORITHMARRAY_H_
  3. #define _ALGORITHMARRAY_H_
  4.  
  5. #define WRONG_POS -9999
  6. #include <iostream>
  7. #include <cstdlib>
  8.  
  9. template <typename T>
  10. class AlgorithmArray
  11. {
  12. private:
  13.  
  14. T *array;
  15. int length;
  16.  
  17. bool checkPos(int pos);
  18. void resizeArray();
  19.  
  20. public:
  21.  
  22. AlgorithmArray();
  23. AlgorithmArray(int length);
  24. AlgorithmArray(const AlgorithmArray<T> &array);
  25. ~AlgorithmArray();
  26.  
  27. void setValue (int pos, T value);
  28. void exchange (int pos1, int pos2);
  29. void removeElement (int pos);
  30. void print ();
  31. T getValue (int pos);
  32. int getLength ();
  33. };
  34.  
  35.  
  36.  
  37.  
  38. #endif
  39.  
  40. //ALG ARRAY IMPL
  41. #include "AlgorithmArray.h"
  42.  
  43. template <typename T>
  44. AlgorithmArray<T>::AlgorithmArray() : AlgorithmArray(1)
  45. {
  46.  
  47. }
  48.  
  49. template <typename T>
  50. AlgorithmArray<T>::AlgorithmArray(int length)
  51. {
  52. this->array = new T[length + 1];
  53. this->length = length;
  54. }
  55.  
  56. template <typename T>
  57. AlgorithmArray<T>::AlgorithmArray(const AlgorithmArray<T> &array)
  58. {
  59. this->array = array.array;
  60. this->length = array.length;
  61. }
  62.  
  63. template <typename T>
  64. AlgorithmArray<T>::~AlgorithmArray()
  65. {
  66. // delete[] this->array;
  67. }
  68.  
  69. template <typename T>
  70. bool AlgorithmArray<T>::checkPos(int pos)
  71. {
  72. return (pos > this->length || pos < 0) ? false : true;
  73. }
  74.  
  75. template <typename T>
  76. void AlgorithmArray<T>::resizeArray()
  77. {
  78. T *temp = new T[this->length * 2 + 1];
  79.  
  80. for (int i = 1; i <= this->length; i++)
  81. {
  82. temp[i] = this->array[i];
  83. }
  84.  
  85. delete[] this->array;
  86.  
  87. this->array = temp;
  88. this->length = 2 * this->length;
  89. }
  90.  
  91. template <typename T>
  92. void AlgorithmArray<T>::setValue(int pos, T value)
  93. {
  94. if (!checkPos(pos)) {
  95. this->resizeArray();
  96. }
  97. this->array[pos] = value;
  98. }
  99.  
  100. template <typename T>
  101. void AlgorithmArray<T>::exchange(int pos1, int pos2)
  102. {
  103. T temp = this->array[pos1];
  104. this->array[pos1] = this->array[pos2];
  105. this->array[pos2] = temp;
  106. }
  107.  
  108. template <typename T>
  109. void AlgorithmArray<T>::removeElement(int pos)
  110. {
  111. if (checkPos(pos)) {
  112. for (int i = pos; i < length; i++)
  113. this->array[i] = this->array[i+1];
  114. }
  115. }
  116.  
  117. template <typename T>
  118. void AlgorithmArray<T>::print()
  119. {
  120. std::cout << "Array: ";
  121.  
  122. for (int i = 1; i <= this->length; i++)
  123. {
  124. std::cout << this->array[i] << " ";
  125. }
  126. std::cout << std::endl;
  127. }
  128.  
  129. template <typename T>
  130. T AlgorithmArray<T>::getValue(int pos)
  131. {
  132. if (checkPos(pos)) {
  133. return this->array[pos];
  134. } else {
  135. return WRONG_POS;
  136. }
  137. }
  138.  
  139. template <typename T>
  140. int AlgorithmArray<T>::getLength()
  141. {
  142. return this->length;
  143. }
  144.  
  145. template class AlgorithmArray<int>;
  146.  
  147. //INSERTION SORT HEADER
  148.  
  149. #ifndef _INSERTIONSORT_H_
  150. #define _INSERTIONSORT_H_
  151.  
  152. #include <iostream>
  153.  
  154. #include "../algorithmarray/AlgorithmArray.h"
  155.  
  156. template <typename T>
  157. class InsertionSort
  158. {
  159. private:
  160. AlgorithmArray<T> array;
  161.  
  162. public:
  163.  
  164. InsertionSort();
  165. InsertionSort(int length);
  166. InsertionSort(AlgorithmArray<T> array);
  167.  
  168. void sort();
  169. AlgorithmArray<T> getArray();
  170. };
  171.  
  172. #endif
  173.  
  174. //INSERTION SORT IMPL
  175.  
  176. #include "InsertionSort.h"
  177.  
  178. template <typename T>
  179. InsertionSort<T>::InsertionSort()
  180. {
  181. this->array = AlgorithmArray<T>();
  182. }
  183.  
  184. template <typename T>
  185. InsertionSort<T>::InsertionSort(int length)
  186. {
  187. this->array = AlgorithmArray<T>(length);
  188. }
  189.  
  190. template <typename T>
  191. InsertionSort<T>::InsertionSort(AlgorithmArray<T> array)
  192. {
  193. AlgorithmArray<T> newArray = AlgorithmArray<T>(array.getLength());
  194.  
  195. for (int i = 1; i <= array.getLength(); i++)
  196. {
  197. newArray.setValue(i, array.getValue(i));
  198. }
  199. this->array = newArray;
  200. }
  201.  
  202. template <typename T>
  203. void InsertionSort<T>::sort()
  204. {
  205. int key, i;
  206.  
  207. for (int j = 2; j <= this->array.getLength(); j++)
  208. {
  209. key = this->array.getValue(j);
  210. i = j - 1;
  211.  
  212. while (i > 0 && this->array.getValue(i) > key)
  213. {
  214. this->array.setValue(i + 1, this->array.getValue(i));
  215. i = i - 1;
  216. }
  217.  
  218. this->array.setValue(i + 1, key);
  219. }
  220. }
  221.  
  222. template <typename T>
  223. AlgorithmArray<T> InsertionSort<T>::getArray()
  224. {
  225. return this->array;
  226. }
  227.  
  228. template class InsertionSort<int>;
  229.  
  230. //MAIN
  231.  
  232. #include <iostream>
  233. #include "InsertionSort.h"
  234.  
  235. int main(void)
  236. {
  237. AlgorithmArray<int> array = AlgorithmArray<int>();
  238.  
  239. array.setValue(1, 10);
  240. array.setValue(2, -3);
  241. array.setValue(3, 50);
  242. array.setValue(4, 40);
  243. array.setValue(5, 70);
  244. array.setValue(6, -9);
  245. array.setValue(7, 20);
  246.  
  247. array.print();
  248.  
  249. InsertionSort<int> alg(array);
  250.  
  251. alg.sort();
  252.  
  253. alg.getArray().print();
  254.  
  255. return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement