Advertisement
arthur990807

source code

Apr 1st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.82 KB | None | 0 0
  1. #include "main.h"
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #define RAND_RANGE 100
  8.  
  9. typedef enum {UNSORTED, ACTIVE, PIVOT, SORTED} State;
  10.  
  11. /* Character to display the state with */
  12. char disp(State s)
  13. {
  14.     if(s==UNSORTED) return ' ';
  15.     if(s==ACTIVE) return '=';
  16.     if(s==PIVOT) return '!';
  17.     if(s==SORTED) return '+';
  18.     return '#';
  19. }
  20.  
  21. /* Number length (no itoa calling) */
  22. int intlen(int n)
  23. {
  24.     if(n<0) return intlen(-n)+1;
  25.     if(n>9) return intlen(n/10)+1;
  26.     return 1;
  27. }
  28.  
  29. /* Print array with states */
  30. void print_array(int *a, int _size, State *S)
  31. {
  32.     int i;
  33.     for(i=0;i<_size;i++)
  34.     {
  35.         printf("%i%s ",a[i],(i==_size-1)?"":",");
  36.     }
  37.     printf("\n");
  38.     for(i=0;i<_size;i++)
  39.     {
  40.         printf("%*c  ",intlen(a[i]), disp(S[i]));
  41.     }
  42. }
  43.  
  44. /* Sorting */
  45. void quicksort_iter(int *a, int size, int left, int right, State *S)
  46. {
  47.     int i;
  48.     int small[right-left+1]; /* arrays for things smaller and larger than the pivot; things equal to the pivot get put in large */
  49.     int large[right-left+1];
  50.     printf("Sorting array of %i elements between indices %i and %i\n",size,left,right);
  51.     for(i=left;i<=right;i++)
  52.     {
  53.         S[i] = ACTIVE;
  54.     }
  55.     if(right<left)
  56.     {
  57.         printf("SOMETHING WENT DESPERATELY WRONG\n");
  58.         exit(-1);
  59.         return;
  60.     }
  61.     if(right==left)
  62.     {
  63.         printf("Nothing to sort\n\n\n");
  64.         S[left] = SORTED;
  65.         print_array(a,size,S);
  66.         getch();
  67.         system("cls");
  68.         return;
  69.     }/* the two bounds are equal and there's nothing to sort */
  70.     if(right==left+1)
  71.     {
  72.         printf("Sorting two elements\n\n\n");
  73.         if(a[left]>a[right])
  74.         {
  75.             int t = a[left];
  76.             a[left]=a[right];
  77.             a[right]=t;
  78.         }
  79.         S[left] = S[right] = SORTED;
  80.         print_array(a,size,S);
  81.         getch();
  82.         system("cls");
  83.         return;
  84.     } /* the two bounds are right next to each other and sorting reduces to a swap */
  85.  
  86.     /* it's guaranteed past this point that there exists at least one element between a[left] and a[right]
  87.      * or in other words that left+1 < right */
  88.  
  89.     int pivot = (left+right)/2; /* pick one in the middle */
  90.     int pivot_num = a[pivot]; /* store the pivot number to later put it back in its place */
  91.  
  92.     int small_count = 0, large_count = 0;
  93.  
  94.     for(i=left;i<=right;i++)
  95.     {
  96.         if(i!=pivot)
  97.         {
  98.             if(a[i]<pivot_num)
  99.             {
  100.                 small[small_count] = a[i];
  101.                 small_count++;
  102.             }
  103.             else
  104.             {
  105.                 large[large_count] = a[i];
  106.                 large_count++;
  107.             }
  108.         }
  109.     }
  110.  
  111.     /* debug: probing arrays */
  112.     printf("Pivot: %i\n",pivot_num);
  113.     printf("Small array: ");
  114.     for(i=0;i<small_count;i++)
  115.     {
  116.         printf("%i ",small[i]);
  117.     }
  118.     printf("\n");
  119.     printf("Large array: ");
  120.     for(i=0;i<large_count;i++)
  121.     {
  122.         printf("%i ",large[i]);
  123.     }
  124.     printf("\n");
  125.  
  126.     /* reinserting sorted arrays in place */
  127.     for(i=0;i<small_count;i++)
  128.     {
  129.         a[left+i] = small[i];
  130.     }
  131.     a[left+small_count] = pivot_num;
  132.     for(i=1;i<=large_count;i++)
  133.     {
  134.         a[left+small_count+i] = large[i-1];
  135.     }
  136.     S[left+small_count] = PIVOT;
  137.  
  138.     /* debug: probing new array */
  139.     print_array(a,size,S);
  140.     getch();
  141.     system("cls");
  142.  
  143.     S[left+small_count] = SORTED;
  144.     for(i=0;i<small_count;i++)
  145.     {
  146.         S[left+i] = UNSORTED;
  147.     }
  148.     for(i=1;i<=large_count;i++)
  149.     {
  150.         S[left+small_count+i] = UNSORTED;
  151.     }
  152.  
  153.     if(small_count != 0)
  154.     {
  155.         quicksort_iter(a, size, left, left+small_count-1, S);
  156.     }
  157.     if(large_count != 0)
  158.     {
  159.         quicksort_iter(a, size, left+small_count+1, right, S);
  160.     }
  161. }
  162.  
  163.  
  164. int DLL_EXPORT main(void)
  165. {
  166.     int size, i; /* array size & counter, respectively */
  167.     int *a; /* the array to be sorted */
  168.     State *S; /* state array */
  169.     int flag = 1;
  170.  
  171.     printf("Enter array size: ");
  172.     scanf("%i",&size);
  173.     a = malloc(size * sizeof(int));
  174.     S = malloc(size * sizeof(State));
  175.     for(i=0;i<size;i++)
  176.     {
  177.         S[i] = UNSORTED;
  178.     }
  179.  
  180.     while(flag)
  181.     {
  182.         printf("Press R to generate a random array; press M to input array manually\n");
  183.         switch(getch())
  184.         {
  185.         case 'r':
  186.         case 'R':
  187.             flag = 0;
  188.             srand(time(NULL));
  189.             for(i=0;i<size;i++)
  190.             {
  191.                 a[i] = rand() % (2*RAND_RANGE+1) - RAND_RANGE;
  192.             }
  193.             printf("Randomly generated array: \n");
  194.             for(i=0;i<size;i++)
  195.             {
  196.                 printf("%i ",a[i]);
  197.             }
  198.             printf("\n");
  199.             getch();
  200.             system("cls");
  201.             break;
  202.         case 'm':
  203.         case 'M':
  204.             flag = 0;
  205.             printf("Enter array: ");
  206.             for(i=0;i<size;i++)
  207.             {
  208.                 scanf("%i",&a[i]);
  209.             }
  210.         }
  211.     }
  212.  
  213.     quicksort_iter(a, size, 0, size-1, S);
  214.  
  215.     printf("\n\n\n\nSorted array: ");
  216.     for(i=0;i<size;i++)
  217.     {
  218.         printf("%i ",a[i]);
  219.     }
  220.     printf("\n");
  221.     getchar();
  222.     return 0;
  223. }
  224.  
  225. extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  226. {
  227.     switch (fdwReason)
  228.     {
  229.         case DLL_PROCESS_ATTACH:
  230.             // attach to process
  231.             // return FALSE to fail DLL load
  232.             break;
  233.  
  234.         case DLL_PROCESS_DETACH:
  235.             // detach from process
  236.             break;
  237.  
  238.         case DLL_THREAD_ATTACH:
  239.             // attach to thread
  240.             break;
  241.  
  242.         case DLL_THREAD_DETACH:
  243.             // detach from thread
  244.             break;
  245.     }
  246.     return TRUE; // succesful
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement