Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. /* KOPIEC */
  8.  
  9.  
  10. class Heap {
  11. private:
  12. int* kopiec;
  13. int capacity;
  14. int heap_size;
  15. public:
  16. Heap(int c) {
  17. kopiec = new int[c];
  18. capacity = c;
  19. };
  20.  
  21. Heap() {
  22. kopiec = new int[10];
  23. kopiec[0] = 12;
  24. kopiec[1] = 9;
  25. kopiec[2] = 10;
  26. kopiec[3] = 7;
  27. kopiec[4] = 9;
  28. kopiec[5] = 5;
  29. kopiec[6] = 9;
  30. kopiec[7] = 2;
  31. kopiec[8] = 1;
  32. kopiec[9] = 3;
  33. capacity = 10;
  34. heap_size = 10;
  35. };
  36.  
  37. bool empty() {
  38. if (heap_size <= 0) {
  39. return true;
  40. }
  41. return false;
  42. };
  43.  
  44. bool full() {
  45. if (capacity == heap_size) return true;
  46. return false;
  47. };
  48.  
  49. int left(int i) {
  50. return 2 * i + 1;
  51. };
  52.  
  53. int right(int i) {
  54. return 2 * i + 2;
  55. };
  56.  
  57. int parent(int i) {
  58. return (i - 1) / 2;
  59. };
  60.  
  61. int getSize() {
  62. return heap_size;
  63. };
  64.  
  65. void setHeapSize(int s);
  66.  
  67. int get(int i) {
  68. return kopiec[i];
  69. };
  70.  
  71. void set(int i, int x) {
  72. kopiec[i] = x;
  73. };
  74.  
  75.  
  76. void up(int i) {
  77. if (i > 0) {
  78. int ojciec = parent(i);
  79. if (kopiec[i] > kopiec[ojciec]) {
  80. int temp = kopiec[i];
  81. kopiec[i] = kopiec[ojciec];
  82. kopiec[ojciec] = temp;
  83. up(ojciec);
  84. }
  85. }
  86. };
  87.  
  88.  
  89.  
  90.  
  91. void down(int i) {
  92. int wiekszy;
  93. int leftPos = left(i);
  94. int rightPos = right(i);
  95. if (leftPos <= heap_size && kopiec[leftPos] > kopiec[i]) {
  96. wiekszy = leftPos;
  97. }
  98. else wiekszy = i;
  99.  
  100. if (rightPos <= heap_size && kopiec[rightPos] > kopiec[wiekszy]) {
  101. wiekszy = rightPos;
  102. }
  103.  
  104. if (wiekszy != i) {
  105. int temp = kopiec[i];
  106. kopiec[i] = kopiec[wiekszy];
  107. kopiec[wiekszy] = temp;
  108. down(wiekszy);
  109. }
  110.  
  111. };
  112.  
  113. friend std::ostream& operator<<(std::ostream& out, Heap& h) {
  114. for (int i = 0; i < h.capacity; i++) {
  115. out << "[" << i << "]" << kopiec[i] << endl;
  116. }
  117. return out;
  118. };
  119. };
  120.  
  121. /* HASZOWANIE */
  122.  
  123.  
  124. class HashTable1 {
  125. private:
  126. string* t;
  127. int capacity;
  128. int ht_size;
  129. public:
  130. HashTable1(int c) {
  131. t = new string[c];
  132. capacity = c;
  133. ht_size = 0;
  134. for (int i = 0; i < c; i++) {
  135. t[i] = "";
  136. }
  137. };
  138.  
  139. int hashF(string s) {
  140. int d = s.length;
  141. unsigned int suma = 0;
  142. for (int i = 0; i < d; i++) {
  143. suma += pow(13, d - i - 1) * s[i];
  144. }
  145. return suma % 40;
  146. };
  147.  
  148. void insert(string s) {
  149. int konflikt = 0;
  150. int temp;
  151. temp = hashF(s);
  152. if (t[temp] != "") {
  153. ++konflikt;
  154. ++temp;
  155. while (t[temp] != "") {
  156. ++temp;
  157. temp = temp % 40;
  158. }
  159. t[temp] = s;
  160. ht_size++;
  161. }
  162. else {
  163. t[temp] = s;
  164. ht_size++;
  165. }
  166. };
  167.  
  168. string search(string s) {
  169. int wart;
  170. wart = hashF(s);
  171. if (s == t[wart])
  172. return s;
  173. else
  174. for (int i = 0; i < capacity; i++) {
  175. if (s == t[wart + i])
  176. return s;
  177. }
  178. return "-1";
  179. };
  180.  
  181. int searchs(string s) {
  182. int wart;
  183. wart = hashF(s);
  184. if (s == t[wart])
  185. return wart;
  186. else
  187. for (int i = 0; i < capacity; i++) {
  188. if (s == t[wart + i])
  189. return wart + i;
  190. }
  191. return -1;
  192. };
  193.  
  194. friend ostream& operator<<(ostream& out, HashTable1& ht); //wypisuje tablicę (z numerami pól), pozostawia puste dla wolnych pól
  195. };
  196.  
  197.  
  198.  
  199. /* SORTOWANIE */
  200.  
  201.  
  202.  
  203.  
  204. void prosteWybieranieString(string tab[], int wielkosc) {
  205. for (int i = 0; i < wielkosc; i++) {
  206. int min = i;
  207. for (int j = i + 1; j < wielkosc; j++) {
  208. if (tab[min] < tab[j]) {
  209. min = j;
  210. }
  211. string temp = tab[j];
  212. tab[j] = tab[min];
  213. tab[min] = temp;
  214. }
  215. }
  216.  
  217. }
  218.  
  219.  
  220. void prostaZamianaString(string tab[], int wielkosc) {
  221. for (int i = 0; i < wielkosc; i++) {
  222. for (int j = wielkosc - 1; j > i; j--) {
  223. if (tab[j - 1] > tab[j]) {
  224. string temp = tab[j];
  225. tab[j] = tab[j - 1];
  226. tab[j - 1] = temp;
  227. }
  228. }
  229. }
  230. }
  231.  
  232. void prosteWstawianieString(string tab[], int wielkosc) {
  233. for (int i = 1; i < wielkosc; i++) {
  234. string temp = tab[i];
  235. int j = i - 1;
  236. while (j >= 0 && temp < tab[j]) {
  237. tab[j + 1] = tab[j];
  238. j--;
  239. tab[j + 1] = temp;
  240. }
  241. }
  242. }
  243.  
  244.  
  245. /* GRAFY */
  246.  
  247. struct edge {
  248. int s;
  249. int t;
  250. int weight;
  251. };
  252.  
  253. class Graph {
  254. private:
  255. int** adjMatrix;
  256. int n;
  257. public:
  258. Graph(int n, int m, edge edges[], bool directed = true) {
  259. adjMatrix = new int* [n];
  260. this->n = n;
  261. for (int i = 0; i < n; i++) {
  262. adjMatrix[i] = new int[n];
  263. }
  264. for (int i = 0; i < n; i++) {
  265. for (int j = 0; j < n; j++) {
  266. adjMatrix[i][j] = 0;
  267. }
  268. }
  269. for (int i = 0; i < m; i++) {
  270. adjMatrix[edges[i].s][edges[i].t] = 1;
  271. if (!directed) {
  272. adjMatrix[edges[i].t][edges[i].s] = 1;
  273. }
  274. }
  275. };
  276.  
  277. int edgeCnt() {
  278. int edgesCount = 0;
  279. for (int i = 0; i < n; i++) {
  280. for (int j = 0; j < n; j++) {
  281. if (adjMatrix[i][j] == 1) {
  282. edgesCount++;
  283. }
  284. }
  285. }
  286. return edgesCount;
  287. };
  288.  
  289. int nodeCnt() {
  290. return n;
  291. };
  292.  
  293. void insertEdge(int u, int v) {
  294. adjMatrix[u][v] = 1;
  295. };
  296.  
  297. void deleteEdge(int u, int v) {
  298. adjMatrix[u][v] = 0;
  299. };
  300.  
  301. bool check(int u, int v) {
  302. if (adjMatrix[u][v] == 1)
  303. return true;
  304. return false;
  305. };
  306.  
  307. void bfs(int s) {
  308. bool* isVisited = new bool[n] {false};
  309. queue<int> q;
  310. isVisited[s] = true;
  311. q.push(s);
  312. cout << "[" << s << "] ";
  313.  
  314. while (!q.empty()) {
  315. int x = q.front();
  316. q.pop();
  317. for (int i = 0; i < n; i++) {
  318. if (adjMatrix[x][i] == 1 && !isVisited[i]) {
  319. q.push(i);
  320. isVisited[i] = true;
  321. cout << "[" << i << "] ";
  322. }
  323. }
  324. }
  325. };
  326.  
  327. void dfs(int s) {
  328. static bool* isVisited = new bool[n] {false};
  329. isVisited[s] = true;
  330. cout << "[" << s << "] ";
  331.  
  332. for (int i = 0; i < n; i++) {
  333. if (adjMatrix[s][i] == 1 && !isVisited[i]) {
  334. dfs(i);
  335. }
  336. }
  337. }
  338.  
  339. friend ostream& operator<<(ostream& out, Graph& g) {
  340. for (int i = 0; i < g.n; i++) {
  341. for (int j = 0; j < g.n; j++) {
  342. out << "[" << i << "][" << j << "] " << g.adjMatrix[i][j] << endl;
  343. }
  344. }
  345. return out;
  346. };
  347.  
  348. ~Graph() {
  349. delete[]adjMatrix;
  350. };
  351.  
  352. int find(int d[], int S[]) {
  353.  
  354. }
  355. };
  356.  
  357.  
  358.  
  359.  
  360. int main()
  361. {
  362. string test[] = { "k", "d", "a", "w", "o", "p", "l"};
  363. prosteWstawianieString(test, 7);
  364. for (int i = 0; i < 7; i++) {
  365. cout << "[" << i << "] " << test[i] << endl;
  366. }
  367.  
  368.  
  369. return 0;
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement