Advertisement
Guest User

Untitled

a guest
Nov 13th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <windows.h>
  5. #include <array>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. int DELKA = 20;
  12.  
  13. struct args
  14. {
  15.     int * pole;
  16.     int s;
  17.     int e;
  18. };
  19.  
  20. void vypisPole(int pole[], int size);
  21. int arrayLen(int p[]);
  22.  
  23. int * setridPole(int pole[], int size, int start, int end) {
  24.  
  25.     int *setrizenePole;
  26.     setrizenePole = new int[DELKA/4];
  27.     int tmp2 = start;
  28.  
  29.     for (start; start < end - 1; start++) {
  30.         int maxIndex = start;
  31.         for (int j = start + 1; j < ((tmp2) + (size/4)); j++) { //jede do ctvrtiny delky od zacatku pole, pouzivam tmp2, protoze v ni je puvodni hodnota startu, ktery se inkrementuje v hornim foru
  32.             if (pole[j] > pole[maxIndex]) maxIndex = j;
  33.         }
  34.  
  35.         int tmp = pole[start];
  36.         pole[start] = pole[maxIndex];
  37.         pole[maxIndex] = tmp;
  38.  
  39.     }
  40.  
  41.     start = tmp2;
  42.  
  43.     {
  44.         int i = 0;
  45.  
  46.     for (start ; start < end; start++)
  47.         {
  48.             setrizenePole[i] = pole[start];
  49.             i++;
  50.         }
  51.     }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.     return setrizenePole;
  58.  
  59. }
  60.  
  61. int * naplnPole(int *p, int size)
  62. {
  63.     for (int i = 0; i < size ; i++ )
  64.     {
  65.         p[i] = rand() % 20 - 10;
  66.     }
  67.     return p;
  68. }
  69.  
  70. void vypisPole(int pole[], int size){
  71.    for (int i = 0; i < size; i++){
  72.        printf("%d \n", pole[i]);
  73.    }
  74.    printf("\n\n ");
  75. }
  76.  
  77. /*int * SpojPole(int p1[], int p2[])
  78. {
  79.     int *SpojenePole;
  80.     int velikost = ( sizeof(p1) / sizeof(p1[0]) ) * 2;
  81.     SpojenePole = new int[velikost];
  82.  
  83.     for(int i=0; i< (velikost); i++)
  84.     {
  85.         if(p1[i] > p2[i])
  86.         {
  87.             SpojenePole[i] = p1[i];
  88.         }
  89.         else
  90.         {
  91.             SpojenePole[i] = p2[i];
  92.         }
  93.     }
  94.     return SpojenePole;
  95.  
  96. }*/
  97.  
  98. int * SpojPole(int p1[], int p2[])
  99. {
  100.     int *SpojenePole;
  101.     //int velikost = ( sizeof(p1) / sizeof(p1[0]) );
  102.     int velikost = 5;
  103.     SpojenePole = new int[velikost*2];
  104.  
  105.     int i = 0;
  106.     int j = 0;
  107.  
  108.     int d1 = velikost;
  109.     int d2 = velikost;
  110.  
  111.     while ((i < d1) && (j < d2)) {
  112.     // dosazeni toho mensiho prvku z obou poli a posunuti indexu
  113.         if (p1[i] > p2[j]) {
  114.           SpojenePole[i + j] = p1[i];
  115.           i++;
  116.  
  117.         }
  118.         else {
  119.           SpojenePole[i + j] = p2[j];
  120.           j++;
  121.         }
  122.     }
  123.  
  124.     if (i < d1) {
  125.         while (i < d1) {
  126.           SpojenePole[i + j] = p1[i];
  127.           i++;
  128.         }
  129.     }
  130.     else {
  131.         while (j < d2) {
  132.           SpojenePole[i + j] = p2[j];
  133.           j++;
  134.         }
  135.     }
  136.  
  137.  
  138.  
  139.  
  140.     return SpojenePole;
  141.  
  142. }
  143.  
  144.  
  145. int arrayLen(int p[])
  146. {
  147.    return(sizeof(p)/sizeof(*p));
  148. }
  149.  
  150. int main()
  151. {
  152.     int *p;
  153.  
  154.     p = new int[DELKA];
  155.  
  156.     naplnPole(p, DELKA);
  157.     vypisPole(p, DELKA);
  158.  
  159.     int *policko1,*policko2;
  160.     policko1 = new int[DELKA/4];
  161.     policko2 = new int[DELKA/4];
  162.  
  163.  
  164.  
  165.     args *arg1 = new args;
  166.     args *arg2 = new args;
  167.     args *arg3 = new args;
  168.     args *arg4 = new args;
  169.  
  170.  
  171.     arg1 -> pole = p;
  172.     arg1 -> s = 0;
  173.     arg1 -> e = DELKA/4;
  174.  
  175.     arg2 -> pole = p;
  176.     arg2 -> s = DELKA/4;
  177.     arg2 -> e = DELKA/2;
  178.  
  179.     arg3 -> pole = p;
  180.     arg3 -> s = DELKA/2;
  181.     arg3 -> e = (DELKA/4)*3;
  182.  
  183.     arg4 -> pole = p;
  184.     arg4 -> s = (DELKA/4)*3;
  185.     arg4 -> e = DELKA;
  186.  
  187.  
  188.     //policko = setridPole(pole, DELKA, 0, DELKA/4);
  189.     //setridPole(pole, DELKA, 0, DELKA/4);
  190.     //vypisPole( setridPole(arg1->pole, DELKA, arg1->s, arg1->e), DELKA/4);
  191.  
  192.     //vypisPole( setridPole(arg2->pole, DELKA, arg2->s, arg2->e), DELKA/4);
  193.  
  194.     policko1 = setridPole(arg1->pole, DELKA, arg1->s, arg1->e);
  195.  
  196.     policko2 = setridPole(arg2->pole, DELKA, arg2->s, arg2->e);
  197.  
  198.     vypisPole( setridPole(arg3->pole, DELKA, arg3->s, arg3->e), DELKA/4);
  199.  
  200.     vypisPole( setridPole(arg4->pole, DELKA, arg4->s, arg4->e), DELKA/4);
  201.  
  202.     vypisPole( policko1, DELKA/4);
  203.  
  204.     vypisPole( SpojPole(policko1, policko2), DELKA/2);
  205.  
  206.  
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement