Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int array_with_ints[] = { 1,15,231,2321,655,898,56,3,4,9 };
  8. char array_with_characters[] = { 'p','K','v','#','b','j','C','A','k','>' };
  9. string array_with_strings[] = { "Ala","Ma","Kota","Zatem","Kot","Ma","Ale","Nie","Wiem","Nic" };
  10.  
  11. template <typename pattern>
  12. void bubble_sort(pattern *array,int size)
  13. {
  14. for (int i = 1; i<size; ++i)
  15. for (int j = size - 1; j>0; --j)
  16. {
  17. if (array[j]>array[j - 1])
  18. {
  19. pattern tmp = array[j - 1];
  20. array[j - 1] = array[j];
  21. array[j] = tmp;
  22. }
  23. }
  24. }
  25.  
  26. template <typename pattern>
  27. void insert_sort(pattern *array,int size)
  28. {
  29. pattern pom;
  30. int j;
  31. for (int i = 1; i < size; i++)
  32. {
  33. pom = array[i];
  34. j = i - 1;
  35. while (j >= 0 && array[j]>pom)
  36. {
  37. array[j + 1] = array[j];
  38. --j;
  39. }
  40. array[j + 1] = pom;
  41. }
  42. }
  43.  
  44. template <typename pattern>
  45. void quicksort(pattern *array, int left, int right)
  46. {
  47. pattern v = array[(left + right) / 2];
  48. int i, j;
  49. pattern x;
  50. i = left;
  51. j = right;
  52. do
  53. {
  54. while (array[i]<v) i++;
  55. while (array[j]>v) j--;
  56. if (i <= j)
  57. {
  58. x = array[i];
  59. array[i] = array[j];
  60. array[j] = x;
  61. i++;
  62. j--;
  63. }
  64. } while (i <= j);
  65. if (j>left) quicksort(array, left, j);
  66. if (i<right) quicksort(array, i, right);
  67. }
  68.  
  69. int read()
  70. {
  71. int check;
  72. for (;;)
  73. {
  74. cin >> check;
  75. if (cin.fail())
  76. {
  77. cout << "Nie podano liczby!" << endl;
  78. cin.clear();
  79. cin.ignore();
  80. }
  81. else
  82. break;
  83. }
  84. return check;
  85. }
  86.  
  87. void sort_menu()
  88. {
  89. cout << "\tWYBIERZ ALGORYTM SORTOWANIA" << endl;
  90. cout << "[1] Sortowanie babelkowe" << endl;
  91. cout << "[2] Sortowanie przez wstawianie" << endl;
  92. cout << "[3] Sortowanie quicksort" << endl;
  93. cout << "[0] Wyjscie" << endl;
  94.  
  95. }
  96.  
  97. void type_menu()
  98. {
  99. cout << "\tWYBIERZ TYP DANYCH" << endl;
  100. cout << "[1] INT" << endl;
  101. cout << "[2] CHAR" << endl;
  102. cout << "[3] STRING" << endl;
  103. }
  104.  
  105. template <typename pattern>
  106. void copy(pattern *array, pattern *buffor_array)
  107. {
  108. for (int i = 0; i < 10; i++)
  109. buffor_array[i] = array[i];
  110. }
  111. template <typename pattern>
  112. void print(pattern *array)
  113. {
  114. for (int i = 0; i<10; i++)
  115. {
  116. cout << array[i] << " ";
  117. }
  118. cout << "\n";
  119. }
  120.  
  121. void menu(int choice, int check)
  122. {
  123. if(check == 1)
  124. {
  125. cout << "Tablica przed sortowaniem: " << endl;
  126. print(array_with_ints);
  127. cout << endl;
  128. int buffor_array_with_ints[10]={0};
  129. copy(array_with_ints, buffor_array_with_ints);
  130. cout << endl;
  131. if (choice == 1)
  132. {
  133. bubble_sort(buffor_array_with_ints, 10);
  134. }
  135. if (choice == 2)
  136. {
  137. insert_sort(buffor_array_with_ints, 10);
  138. }
  139. if (choice == 3)
  140. {
  141. quicksort(buffor_array_with_ints,0,9);
  142. }
  143. cout << "Tablica po sortowaniu: " << endl;
  144. print(buffor_array_with_ints);
  145.  
  146. }
  147. if(check == 2)
  148. {
  149. cout << "Tablica przed sortowaniem: " << endl;
  150. print(array_with_characters);
  151. cout << endl;
  152. char buffor_array_with_characters[10]={0};
  153. copy(array_with_characters, buffor_array_with_characters);
  154. cout << endl;
  155. if (choice == 1)
  156. {
  157. bubble_sort(buffor_array_with_characters, 10);
  158. }
  159. if (choice == 2)
  160. {
  161. insert_sort(buffor_array_with_characters, 10);
  162. }
  163. if (choice == 3)
  164. {
  165. quicksort(buffor_array_with_characters,0,9);
  166. }
  167. cout << "Tablica po sortowaniu: " << endl;
  168. print(buffor_array_with_characters);
  169.  
  170. }
  171. if(check == 3)
  172. {
  173. cout << "Tablica przed sortowaniem: " << endl;
  174. print(array_with_strings);
  175. cout << endl;
  176. string buffor_array_with_strings[10]={ " " };
  177. copy(array_with_strings,buffor_array_with_strings);
  178. cout << endl;
  179. if (choice == 1)
  180. {
  181. bubble_sort(buffor_array_with_strings, 10);
  182. }
  183. if (choice == 2)
  184. {
  185. insert_sort(buffor_array_with_strings, 10);
  186. }
  187. if (choice == 3)
  188. {
  189. quicksort(buffor_array_with_strings,0,9);
  190. }
  191. cout << "Tablica po sortowaniu: " << endl;
  192. print(buffor_array_with_strings);
  193.  
  194. }
  195. else if(check>3||check<1)
  196. cout << "Nie ma takiej opcji" << endl;
  197.  
  198. }
  199.  
  200. int main()
  201. {
  202. while(1)
  203. {
  204. int choice,check;
  205. sort_menu();
  206. choice = read();
  207. switch(choice)
  208. {
  209. case 1:
  210. type_menu();
  211. check = read();
  212. menu(choice,check);
  213. break;
  214. case 2:
  215. type_menu();
  216. check = read();
  217. menu(choice,check);
  218. break;
  219. case 3:
  220. type_menu();
  221. check = read();
  222. menu(choice,check);
  223. break;
  224. case 0:
  225. exit(0);
  226. default:
  227. cout << "Bledny wybor." << endl;
  228. break;
  229.  
  230. }}
  231. return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement