Advertisement
Guest User

Sorting for Sheiker with process

a guest
Oct 26th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #define SWAP(A,B) {(A)=(A)^(B); (B)=(A)^(B); (A)=(A)^(B);}
  6.  
  7. int getLen(FILE *in) {
  8.     int size;
  9.  
  10.     fscanf(in, "%d", &size);
  11.     return size;
  12. }
  13.  
  14. void arrayFill(FILE *in, int array[], int limit) {
  15.     int i = 0;
  16.     for ( i ; i < limit && fscanf(in, "%d", &array[i]) > 0; i++ );
  17. }
  18.  
  19. void arrayPrint(FILE *out, int array[], int size) {
  20.     size = size - 1;
  21.     int i = 0;
  22.     for (  i; i < size; i++ ) {
  23.         fprintf(out, "%d ", array[i]);
  24.     }
  25.     fprintf(out, "%d\n", array[size]);
  26. }
  27.  
  28.  
  29. void arraySort(int mas[], int n)
  30. {
  31.         int last = n-1, left = 1, right = n-1, j;
  32.  
  33.         do
  34.         {
  35.                 for(j = right; j >= left; j--)
  36.                 {
  37.                         if(mas[j-1] > mas[j])
  38.                         {
  39.                                 SWAP(mas[j-1], mas[j]);
  40.                                 last = j;
  41.                         }
  42.                 }
  43.  
  44.                 left = last + 1;
  45.  
  46.                 for(j = left; j <= right; j++)
  47.                 {
  48.                         if(mas[j-1] > mas[j])
  49.                         {
  50.                                 SWAP(mas[j-1], mas[j]);
  51.                                 last = j;
  52.                         }
  53.                 }
  54.  
  55.                 right = last-1;
  56.  
  57.         } while(left < right);
  58. }
  59.  
  60. int main() {
  61.     FILE *in = fopen("task.in", "r");
  62.     FILE *out = fopen("task.out", "w");
  63.     int len1 = getLen(in);
  64.     int len2 = getLen(in);
  65.     int size = len1 + len2;
  66.     int array1[len1], array2[len2], dst[size];
  67.     int indexPosition;
  68.    
  69.     arrayFill(in, array1, len1);
  70.     arrayFill(in, array2, len2);
  71.     fclose(in);
  72.    
  73.    int  i = 0;
  74.     for ( i; i < len1; i++ ) {
  75.         dst[i] = array1[i];
  76.     }
  77.     i = 0;
  78.     for (i; i < len2; i++ ) {
  79.         indexPosition = len1 + i;
  80.         dst[indexPosition] = array2[i];
  81.     }
  82.    
  83.    A:arraySort(array1, len1);
  84.    B:arraySort(array2, len2);
  85.    short count;
  86.    for (count=0; count < 1; count++) {
  87.      pid_t pid = fork();
  88.      if (pid == 0)
  89.       {
  90.         if (count == 0)
  91.         {
  92.             goto A;
  93.         }
  94.         if (count == 1)
  95.         {
  96.             goto B;
  97.         }
  98.      }
  99.    }
  100.     arraySort(dst, size);
  101.     arrayPrint(out, dst, size);
  102.     fclose(out);
  103.    
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement