Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <windows.h>
  8. #include <cstdlib>//Didn't have these.
  9. #include <ctime> //Didn't have these. Page 289
  10.  
  11. using namespace std;
  12.  
  13.  
  14. template <class Type>
  15. class arraySort{
  16. protected:
  17. //Type arr[75];
  18. Type *arr;
  19. int count;
  20. int length;
  21.  
  22. public:
  23. arraySort();
  24. void add(const Type& value);
  25. virtual void sort() = 0;
  26. void print();
  27.  
  28. private:
  29. //void gotoxy( short x, short y);
  30. };
  31.  
  32. template <class Type>
  33. arraySort<Type>::arraySort(){
  34. count = 0;
  35. arr = new Type[75];
  36. }
  37.  
  38. template <class Type>
  39. void arraySort<Type>::add(const Type& value){
  40. //int temp = 0;
  41. //for(int i = 0; i < 75; i++){
  42. //temp = (rand() % 20);
  43. arr[count] = value;
  44. //}
  45. count++;
  46.  
  47. //for (int i = 0; i < 75; i++){
  48. //temp = (rand() % 20);
  49. }
  50.  
  51. void gotoxy(short x, short y) {
  52. HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  53. COORD position = { x, y };
  54. SetConsoleCursorPosition( hStdout, position );
  55. }
  56.  
  57. template <class Type>
  58. void arraySort<Type>::print(){
  59. system("cls");
  60. for(int i = 0; i < 75; i++){
  61. gotoxy(i,arr[i]);
  62. cout <<"*";
  63. Sleep(10);
  64. }//
  65. //Sleep(10);
  66. }
  67.  
  68. /*
  69. clear screen
  70. for i=0 to 74
  71. {
  72. gotoxy(i,arr[i])
  73. print *
  74. }
  75. */
  76.  
  77. //template <class Type>
  78. //void arraySort<Type>::print(short index, short value){
  79. // system("cls");
  80. // gotoxy(index,value);
  81. // cout <<"*";
  82. // Sleep(10);
  83. //
  84. //}//
  85.  
  86.  
  87.  
  88. template <class Type>
  89. class insertionSort : public arraySort<Type> {
  90. public:
  91. void sort();
  92. };//end class insertionSort
  93.  
  94. template <class Type>
  95. void insertionSort<Type>::sort(){//
  96.  
  97. for (int i = 0; i < 75; i++){
  98. //{print(i, arr[i]);}
  99. //****print();
  100. }
  101. int firstOutOfOrder;
  102. int location;
  103. Type temp;
  104.  
  105. for (firstOutOfOrder = 1; firstOutOfOrder < 75; firstOutOfOrder++)
  106. if(arr[firstOutOfOrder] < arr[firstOutOfOrder - 1]){
  107. temp = arr[firstOutOfOrder];
  108. location = firstOutOfOrder;
  109.  
  110. do{
  111. arr[location] = arr[location - 1];
  112. //print(location, arr[location]);
  113. print();
  114. location--;
  115. }while(location > 0 && arr[location - 1] > temp);
  116. arr[location] = temp;
  117. //print(location, arr[location]);
  118. //print();
  119. }
  120. }//end method iSort
  121.  
  122. //The heapSort class, which inherits off the arraySort base class
  123. template <class Type>
  124. class heapSort : public arraySort<Type> {
  125. public:
  126. void sort();
  127.  
  128. private:
  129. void heapify(int low, int high);
  130. void buildHeap();
  131. };//end class heapSort
  132.  
  133. template <class Type>
  134. void heapSort<Type>::sort(){//write this may havfe to add paraments, call print() at beginning and often
  135. //get code from book
  136. for (int i = 0; i < 75; i++){
  137. //{print(i, arr[i]);}
  138. print();
  139. }
  140. int lastOutOfOrder;
  141. Type temp;
  142. buildHeap();
  143. for (lastOutOfOrder = 75 - 1; lastOutOfOrder >=0; lastOutOfOrder--){
  144. temp = arr[lastOutOfOrder];
  145. arr[lastOutOfOrder] = arr[0];
  146. //print(lastOutOfOrder,arr[lastOutOfOrder]);
  147. print();
  148. arr[0] = temp;
  149. heapify(0, lastOutOfOrder - 1);
  150. }//end for
  151.  
  152. }//end method hSort
  153.  
  154. template <class Type>
  155. void heapSort<Type>::heapify(int low, int high){//write this may havfe to add paraments, call print() at beginning and often
  156. //get code from book
  157. int largeIndex;
  158. Type temp = arr[low];
  159. largeIndex = 2*low + 1;
  160. while(largeIndex <= high)
  161. {
  162. if(largeIndex < high)
  163. if(arr[largeIndex] < arr[largeIndex + 1]){
  164. largeIndex = largeIndex + 1;
  165. //print(largeIndex, arr[largeIndex]);
  166. print();
  167. }
  168. if(temp > arr[largeIndex])
  169. break;
  170. else
  171. {
  172. arr[low] = arr[largeIndex];
  173. //print(low, arr[low]);
  174. print();
  175. low = largeIndex;
  176. largeIndex = 2*low + 1;
  177. }
  178. }//end while
  179. arr[low] = temp;
  180. //print(low, arr[low]);
  181. print();
  182.  
  183. }
  184.  
  185. template <class Type>
  186. void heapSort<Type>::buildHeap(){
  187.  
  188. int index;
  189. for(index = 75/2 + 1; index >= 0; index--)
  190. heapify(index, 75-1);
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197. int main(){
  198.  
  199. insertionSort<int> insS;
  200. heapSort<int> heapS;
  201. int selection;
  202. int temp;
  203. srand(time(0));
  204. for (int i = 0; i < 75; i++){
  205. temp = (rand() % 20);
  206. insS.add(temp);
  207. heapS.add(temp);
  208. }//end for
  209. cout << "Enter 1 for Insertion Sort, 2 for Heap Sort, 3 to quit:" << endl;
  210. cin >> selection;
  211. if (selection == 1)
  212. //do insertion Sort
  213. insS.sort();
  214. if (selection == 2)
  215. //do heapSort
  216. heapS.sort();
  217. if (selection == 3)
  218. cout << "The answer to everything is 42. Exiting program" << endl;
  219. //}
  220. system("pause");
  221. return 0;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement