Facebook
From Blush Lion, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 111
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<time.h>
  4. #include<stdlib.h>
  5. typedef enum { false, true } bool;
  6. void citire(int v[], int* n)
  7. {
  8.  
  9.         srand(time(NULL));
  10.         *n = rand() % 20;
  11.         printf("Vectorul va avea %d elemente\n", *n);
  12.         for (int i = 0; i < *n; i++)
  13.         {
  14.                 v[i] = rand() % 50;
  15.         }
  16.  
  17. }
  18. void afisare(int v[], int N)
  19. {
  20.         int i;
  21.         for (i = 0; i < N; i++)
  22.         {
  23.                 printf("%d ", v[i]);
  24.         }
  25.         printf("\n");
  26. }
  27. void afisare1(int v[], int N)
  28. {
  29.         int i;
  30.         for (i = 1; i <= N; i++)
  31.         {
  32.                 printf("%d ", v[i]);
  33.         }
  34.         printf("\n");
  35. }
  36. void shellsort(int v[], int N)
  37. {
  38.         int h[] = { 3, 2, 1 };
  39.         int i, j, k, w, aux;
  40.         for (w = 0; w < 3; w++)
  41.         {
  42.                 k = h[w];
  43.                 for (i = k; i < N; i=i+k)
  44.                 {
  45.                         int aux = v[i];
  46.                         int j = i - k;
  47.                         while (j >= 0 && v[j] > aux)
  48.                         {
  49.                                 v[j + k] = v[j];
  50.                                 j=j-k;
  51.                         }
  52.                         v[j + k] = aux;
  53.                 }
  54.         }
  55. }
  56.  
  57.  
  58. void deplasare(int v[], int s, int d,int N)
  59. {
  60.         int i, j, x;
  61.         int b = 0;
  62.  
  63.         i = s;
  64.         j = 2 * i;
  65.         x = v[i];
  66.  
  67.         while ((j <= d) && (b == 0))
  68.         {
  69.                 if (j < d)
  70.                 {
  71.                         if (v[j] < v[j + 1])
  72.                                 j++;
  73.                 }
  74.  
  75.                 if (x < v[j])
  76.                 {
  77.                         v[i] = v[j];
  78.                         i = j;
  79.                         j = 2 * i;
  80.                 }
  81.                 else
  82.                 {
  83.                         b = 1;
  84.                 }
  85.                 afisare1(v,N );
  86.         }
  87.         v[i] = x;
  88. }
  89. void heapsort(int v[], int n)
  90. {
  91.         int s, d, aux;
  92.        
  93.         s = n / 2 + 1;
  94.         d = n;
  95.  
  96.         while (s > 1)
  97.         {
  98.                 s--;
  99.                 deplasare(v, s, n,n);
  100.         }
  101.  
  102.         while (d > 1)
  103.         {
  104.                 aux = v[1];
  105.                 v[1] = v[d];
  106.                 v[d] = aux;
  107.                 d--;
  108.                 deplasare(v, 1, d,n);
  109.         }
  110. }
  111. void quicksort(int a[], int s, int d)
  112. {
  113.         int i=s, j=d, x;
  114.         int aux;
  115.  
  116.         x = a[(s + d) / 2];
  117.         do
  118.         {
  119.                 while (a[i] <= x && i< d)
  120.                 {
  121.                         i++;
  122.                 }
  123.                 while (a[j] > x && j>s)
  124.                 {
  125.                         j--;
  126.                 }
  127.                 if (i <= j)
  128.                 {
  129.                    aux = a[i];
  130.                         a[i] = a[j];
  131.                         a[j] = aux;
  132.                         i++;
  133.                         j--;
  134.                 }
  135.  
  136.         } while (i <= j);
  137.         if (j > s)
  138.         {
  139.                 quicksort(a, s, j);
  140.         }
  141.         if (i < d)
  142.         {
  143.                 quicksort(a, i, d);
  144.         }
  145.  
  146. }
  147. void mutare(int a[], int b[], int N)
  148. {
  149.         for (int i = 0; i < N; i++)
  150.         {
  151.                 b[i + 1] = a[i];
  152.         }
  153. }
  154. int main()
  155. {
  156.         int v[100], v1[100], N, opt;
  157.        
  158.         citire(v, &N);
  159.         afisare(v, N);
  160.         do {
  161.                 printf("0. Iesire\n1. ShellSort\n2. Heapsort\n3. Selectie\n4. Selectie Performanta\n5. Interschimbare\n6. Shakesort\n");
  162.                 printf("Alegeti optiune: ");
  163.                 scanf("%d", &opt);
  164.                 switch (opt)
  165.                 {
  166.                 case 1:
  167.                         shellsort(v, N);
  168.                         printf("Vectorul sortat prin ShellSort este: \n");
  169.                         afisare(v, N);
  170.                         break;
  171.                 case 2:
  172.                         mutare(v, v1, N);
  173.                         heapsort(v1, N);
  174.                         printf("Vectorul sortat prin Heapsort este: \n");
  175.                         afisare1(v1,N);
  176.                         break;
  177.                 case 3:
  178.                         mutare(v, v1, N);
  179.                         quicksort(v1,1,N);
  180.                         printf("Vectorul sortat prin Quicksort este: \n");
  181.                         afisare1(v1, N);
  182.                         break;
  183.                
  184.                 }
  185.         } while (opt);
  186.  
  187.         return 0;
  188.  
  189. }