Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #define MAXINT 32000
  4. #define BASE 10
  5.  
  6. void printvek(int * vek, int n )
  7. {   int i;
  8.         for(i=0; i<n; i++ )
  9.                 printf("%i ", vek[i]);
  10.         printf("\n");
  11. }
  12.  
  13. int * genvek(int n, int x, int lim)
  14. {   int i;
  15.        int * vek = (int*)malloc( sizeof(int) * n );
  16.  
  17.         if (x == 1)
  18.             {for( i=0; i<=n; i++)
  19.                 vek[i] = i+1;
  20.             }
  21.  
  22.        else
  23.             if (x == 2)
  24.             {srand( (unsigned)time(NULL) );
  25.             for( i=0 ; i<n ; i++ )
  26.                 vek[i] = rand() % lim + 1;
  27.             }
  28.  
  29.        else
  30.             if (x == 3)
  31.                 {for( i=0; i<n; i++)
  32.                     vek[i] = n - i;
  33.                 }
  34. return vek;
  35.         }
  36.  
  37.  
  38. struct node {
  39.     int val;
  40.     struct node * left;
  41.     struct node * right;
  42. };
  43.  
  44. void wrb( struct node * rad , int * vect )
  45. {
  46.     static int count = 0;
  47.  
  48.     if( rad->left ) wrb( rad->left , vect );
  49.     vect[count++] = rad->val;
  50.     //prn( vect , 20 );
  51.     //printf("(%i):%i\n",count,rad->val);
  52.     //count++;
  53.     if( rad->right ) wrb( rad->right , vect );
  54.  
  55.  
  56. }
  57.  
  58. void binary_search_tree( int * vect , int size )
  59. {   struct node * rad,* nou,* p;
  60.     int i;
  61.  
  62.     rad = (struct node *)malloc( sizeof(struct node) );
  63.  
  64.     rad->val = vect[0];
  65.     rad->left = rad->right = NULL;
  66.  
  67.     for( i=1 ; i<size ; i++ )
  68.     {
  69.         nou = (struct node *)malloc( sizeof(struct node) );
  70.  
  71.         nou->val = vect[i];
  72.         nou->left = nou->right = NULL;
  73.  
  74.         p = rad;
  75.         while(1)
  76.         {
  77.             if( p->val > nou->val  )
  78.                 if( p->left ) p=p->left;
  79.                 else {
  80.                     p->left = nou;
  81.                     break;
  82.                 }
  83.             else
  84.                 if( p->right ) p=p->right;
  85.                 else {
  86.                     p->right = nou;
  87.                     break;
  88.                 }
  89.         }
  90.  
  91.     }
  92.     //bin(rad,0);
  93.  
  94.     //wrb( rad , vect );
  95.     //printf("\n");
  96.     printvek( vect , size );
  97. }
  98.  
  99. void radix_sort( int * vect , int size )
  100. {
  101.     int * temp,step=1,nr_dig[BASE],pos[BASE],i,max=vect[0],start;
  102.  
  103.     temp = (int*)malloc( size * sizeof(int) );
  104.  
  105.     for( i=1 ; i<size ; i++ )
  106.         if( max < vect[i] ) max = vect[i];
  107.  
  108.     while( max > 0 )
  109.     {
  110.         for( i=0 ; i<BASE ; i++ ) nr_dig[i]=0;
  111.  
  112.         for( i=0 ; i<size ; i++ )
  113.             nr_dig[ (vect[i] / step) % BASE ]++;
  114.  
  115.         for( i=1 ; i<BASE ; i++ )
  116.             nr_dig[i] += nr_dig[i-1];
  117.  
  118.         pos[0]=0;
  119.         for( i=0 ; i<BASE-1 ; i++ )
  120.             pos[i+1] = nr_dig[i];
  121.  
  122.         for( i=0 ; i<size ; i++ )
  123.         {
  124.             start = (vect[i] / step) % BASE;
  125.             temp[  pos[start]++  ] = vect[i];
  126.         }
  127.  
  128.         for( i=0 ; i<size ; i++ )
  129.             vect[i] = temp[i] ;
  130.  
  131.         max = max / BASE;
  132.         step = step * BASE;
  133.  
  134.         //prn( nr_dig , BASE );
  135.         printvek(vect,size);
  136.     }
  137. }
  138.  
  139. void cocktail_sort( int * vect , int size )
  140. {
  141.     int swap = 1,i,temp;
  142.     while( swap )
  143.     {
  144.         swap = 0;
  145.         for( i=0 ; i<size-1 ; i++ )
  146.             if( vect[i] > vect[i+1] )
  147.             {
  148.                 temp = vect[i];
  149.                 vect[i] = vect[i+1];
  150.                 vect[i+1] = temp;
  151.  
  152.                 swap = 1;
  153.             }
  154.         if( !swap ) break;
  155.         //swap=0;
  156.         for( i=size-2 ; i>=0 ; i-- )
  157.                         if( vect[i] > vect[i+1] )
  158.                         {
  159.                                 temp = vect[i];
  160.                                 vect[i] = vect[i+1];
  161.                                 vect[i+1] = temp;
  162.  
  163.                 swap = 1;
  164.                         }
  165.  
  166.     }
  167. }
  168.  
  169. int main()
  170. {
  171.     int choice;
  172.     int * v1;
  173.  
  174.     printf("Mod Choice:\n");
  175.     printf("1)Modul de testare:\n2)Modul de analiza:\n");
  176.     scanf("%d",&choice);
  177.     v1 = genvek(10, 2, 100);
  178.  
  179.     //clrscr();
  180.  
  181.     if( choice == 1 )
  182.     {
  183.         printf("Modul analiza -|--\n");
  184.         printvek(v1, 10);
  185.     }
  186.     else
  187.     {
  188.         printf("Modul testare :)\n");
  189.         printvek(v1, 10);
  190.     }
  191.  
  192.     return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement