Advertisement
Guest User

Algoritmi izlazni najbitniji

a guest
Jan 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.58 KB | None | 0 0
  1. ---------------------------------------------------------------------------------
  2. //Pretrazivanje
  3.  
  4. void sekvencijalno(int *polje, int a, int n)
  5. {
  6.     int pronadeno = 0;
  7.     for (int i = 0; i < n; i++)
  8.     {
  9.         if (polje[i] == a)
  10.         {
  11.             printf("Pronadeno");
  12.             pronadeno = 1;
  13.         }
  14.     }
  15.     if (pronadeno == 0)
  16.     {
  17.         printf("Broj nije pronaden");
  18.     }
  19. }
  20.  
  21. void binarno(int* polje, int a, int n)
  22. {
  23.     int dg, gg, s, f;
  24.     dg = 0;
  25.     gg = n - 1;
  26.     s = (dg + gg) / 2;
  27.     f = 0;
  28.     while (dg <= gg) {
  29.         if (polje[s] == a)
  30.         {
  31.             printf("Pronađen");
  32.             f = 1; break;
  33.         }
  34.         else if (polje[s] < a)
  35.         {
  36.             dg = s + 1;
  37.         }
  38.         else if (polje[s] < a)
  39.         {
  40.             gg = s - 1;
  41.         }
  42.         s = (dg + gg) / 2;
  43.     }
  44.     if (f == 0) {
  45.         printf("Nije pronaden");
  46.     }
  47. }
  48.  
  49. // Sortiranja
  50.  
  51. void zamjena(int *a, int *b)
  52. {
  53.     int t = *a;
  54.     *a = *b;
  55.     *b = t;
  56. }
  57.  
  58. void bubblesort(int *polje, int n)
  59. {
  60.     for (int i = 0; i < n - 1; i++) {
  61.         for (int j = 0; j < n - i - 1; j++) {
  62.             if (polje[j] > polje[j + 1])
  63.                 zamjena(&polje[j], &polje[j + 1]);
  64.         }
  65.     }
  66. }
  67.  
  68. void selectionsort(int *polje, int n)
  69. {
  70.     int i, j, minimum;
  71.     for (i = 0; i < n; i++) {
  72.         minimum = i;
  73.         for (j = i + 1; j < n; j++) {
  74.             if (polje[j] < polje[minimum]) {
  75.                 minimum = j;
  76.             }
  77.         }
  78.         zamjena(&polje[i], &polje[minimum]);
  79.     }
  80. }
  81.  
  82. ---------------------------------------------------------------------------------
  83. //Povezani popisi
  84.  
  85. struct element {
  86.     int x;
  87.     struct element *next;
  88. };
  89.  
  90. struct element* lista(int n)
  91. {
  92.     struct element *p = NULL;
  93.     struct element *glava = NULL;
  94.  
  95.     for (int i = 0; i < n; i++)
  96.     {
  97.         if (i == 0)
  98.         {
  99.             glava = ((struct element*)malloc(sizeof(struct element)));
  100.             p = glava;
  101.         }
  102.  
  103.         else
  104.         {
  105.             p->next = ((struct element*)malloc(sizeof(struct element)));
  106.             p = p->next;
  107.         }
  108.  
  109.         scanf_s("%d", &p->x);
  110.     }
  111.  
  112.     p->next = NULL;
  113.     return glava;
  114. }
  115.  
  116. void ispis(struct element* glava)
  117. {
  118.     struct element *p = glava;
  119.     while (p != NULL)
  120.     {
  121.         printf("%d", p->x);
  122.         p = p->next;
  123.     }
  124. }
  125.  
  126. void ubacinakraj(struct element *glava, int n)
  127. {
  128.     struct element* trenutni = glava;
  129.     struct element* novi = NULL;
  130.     while (trenutni->next != NULL)
  131.     {
  132.         trenutni = trenutni->next;
  133.     }
  134.     novi = ((struct element*)malloc(sizeof(struct element)));
  135.     novi->x = n;
  136.     novi->next = NULL;
  137.     trenutni->next = novi;
  138. }
  139.  
  140. void ubacinapoziciju(struct element*glava, int n, int poz)
  141. {
  142.     struct element* novi = NULL;
  143.     struct element* lijevi = NULL;
  144.     struct element* desni = glava;
  145.  
  146.     for (int i = 1; i < poz; i++)
  147.     {
  148.         lijevi = desni;
  149.         desni = desni->next;
  150.     }
  151.     novi = ((struct element*)malloc(sizeof(struct element)));
  152.     novi->x = n;
  153.     lijevi->next = novi;
  154.     lijevi = novi;
  155.     lijevi->next = desni;
  156. }
  157.  
  158. void izbacizadnji(struct element*glava)
  159. {
  160.     struct element *p = glava;
  161.     struct element *prethodni = NULL;
  162.     while (p->next != NULL)
  163.     {
  164.         prethodni = p;
  165.         p = p->next;
  166.     }
  167.     prethodni->next = NULL;
  168.     free(p);
  169. }
  170.  
  171. void izbacipoziciju(struct element*glava, int poz)
  172. {
  173.     struct element *p = glava;
  174.     struct element *prethodni = NULL;
  175.  
  176.     for (int i = 1; i < poz; i++)
  177.     {
  178.         prethodni = p;
  179.         p = p->next;
  180.     }
  181.  
  182.     prethodni->next = p->next;
  183.     free(p);
  184. }
  185.  
  186. int sort(struct element* glava)
  187. {
  188.     struct element* p1 = (struct element*)malloc(sizeof(struct element));
  189.     struct element* p2 = (struct element*)malloc(sizeof(struct element));
  190.     int t = 0;
  191.     for (p1 = glava; p1 != NULL; p1->next++) {
  192.         for (p2 = p1->next; p2 != NULL; p2->next++) {
  193.             if (p1->next > p2->next)
  194.             {
  195.                 t = p1->next;
  196.                 p1->next = p2->next;
  197.                 p2->next = t;
  198.             }
  199.         }
  200.     }
  201. }
  202.  
  203. void provjerasorta(struct element*glava)
  204. {
  205.     int br = 0;
  206.     int usporedba = 0;
  207.     struct element *p = glava;
  208.     while (p != NULL) {
  209.         if (br == 0) {
  210.             usporedba = p->x;
  211.             p = p->next;
  212.         }
  213.         else {
  214.             if (p->x < usporedba)
  215.                 printf("nije");
  216.             break;
  217.         }
  218.         usporedba = p->x;
  219.         p = p->next;
  220.     }
  221. }
  222.  
  223. ---------------------------------------------------------------------------------
  224. //Stogovi
  225.  
  226. #include<stdio.h>
  227. #include<stdlib.h>
  228.  
  229. #define MAX 5
  230. int stack_point = -1;
  231. int stog[5]= { 1,2,3,4, 5};
  232.  
  233. int isEmpty() {
  234.     if (stack_point == -1)
  235.         return 1;
  236.     else
  237.         return 0;
  238. }
  239.  
  240. int isFull() {
  241.     if (stack_point == MAX)
  242.         return 1;
  243.     else
  244.         return 0;
  245. }
  246.  
  247. void push(int a)
  248. {
  249.     if (isFull())
  250.         printf("Pun stog");
  251.     else {
  252.         stack_point += 1;
  253.         stog[stack_point] = a;
  254.     }
  255. }
  256.  
  257. int pop() {
  258.     int rez = -1;
  259.     if (isEmpty()) {
  260.         printf("Prazan");
  261.     }
  262.     else {
  263.         rez = stog[stack_point];
  264.         stack_point--;
  265.     }
  266.     return rez;
  267. }
  268.  
  269. void peek()
  270. {
  271.     if (!isEmpty())
  272.         printf("%d", stog[stack_point]);
  273. }
  274.  
  275. ---------------------------------------------------------------------------------
  276. //Stabla
  277.  
  278. struct cvor {
  279.     int x;
  280.     struct cvor *lijevi;
  281.     struct cvor *desni;
  282. };
  283.  
  284. void ubaci(struct cvor* novi, struct cvor* roditelj)
  285. {
  286.     if ((roditelj->desni == NULL) && (novi->x > roditelj->x)) {
  287.         roditelj->desni = novi;
  288.     }
  289.     else if ((roditelj->desni != NULL)) && (novi->x > roditelj->x)){
  290.     ubaci(roditelj->desni, novi);
  291.     }
  292.     if ((roditelj->lijevi == NULL) && (novi->x < roditelj->x)) {
  293.         roditelj->lijevi = novi;
  294.     }
  295.     else if ((roditelj->lijevi != NULL) && (novi->x < roditelj->x)) {
  296.         ubaci(roditelj->lijevi, novi);
  297.     }
  298. }
  299.  
  300. int prd(struct cvor* korijen)
  301. {
  302.     if (korijen == 0)return 1;
  303.     else {
  304.         printf("%d", korijen->x);
  305.         prd(korijen->desni);
  306.         prd(korijen->lijevi);
  307.     }
  308. }
  309.  
  310. int iod(struct cvor* korijen)
  311. {
  312.     if (korijen == 0)return 1;
  313.     else {
  314.         iod(korijen->lijevi);
  315.         printf("%d", korijen->x);
  316.         iod(korijen->desni);
  317.     }
  318. }
  319.  
  320. int pod(struct cvor* korijen)
  321. {
  322.     if (korijen == 0)return 1;
  323.     else {
  324.         pod(korijen->lijevi);
  325.         pod(korijen->desni);
  326.         printf("%d", korijen->x);
  327.     }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement