Advertisement
Knogle

Sort

Feb 26th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1.  
  2. #include <a_samp>
  3. #include <hex>
  4. //new
  5. //  lilarray[] = { -541, 54, 689, 12, 3, 0, 3, 55, 66, -541, 5468484, -564, 1554, 1656 }
  6. //;
  7. new lilarray[1024];
  8. new bigarray[sizeof(lilarray)];
  9. new heaparray[sizeof(lilarray)];
  10. new bubblearray[sizeof(lilarray)];
  11.  
  12. #pragma tabsize 0
  13. #define START_BENCH(%0); {new __a=%0,__b=0,__c,__d=GetTickCount(),__e=1;do{}\
  14.         while(__d==GetTickCount());__c=GetTickCount();__d=__c;while(__c-__d<__a||\
  15.         __e){if(__e){if(__c-__d>=__a){__e=0;__c=GetTickCount();do{}while(__c==\
  16.                     GetTickCount());__c=GetTickCount();__d=__c;__b=0;}}{
  17.  
  18.             #define FINISH_BENCH(%0); }__b++;__c=GetTickCount();}printf(" Bench for "\
  19.         %0": executes, by average, %.2f times/ms.",floatdiv(__b,__a));}
  20.        
  21.        
  22.  
  23.  
  24. stock Float:frandom(Float:max, Float:min = 0.0, dp = 4)
  25. {
  26.     new
  27.     Float:mul = floatpower(10.0, dp),
  28.     imin = floatround(min * mul),
  29.     imax = floatround(max * mul);
  30.     return float(random(imax - imin) + imin) / mul;
  31. }
  32. stock BubbleSort(array[],length = sizeof(array))
  33.  {
  34.      new i, j;
  35.      for (i = 0; i < length - 1; ++i)
  36.      {
  37.  
  38.         for (j = 0; j < length - i - 1; ++j)
  39.         {
  40.             if (array[j] > array[j + 1])
  41.             {
  42.                 new tmp = array[j];
  43.                 array[j] = array[j + 1];
  44.                 array[j + 1] = tmp;
  45.             }
  46.         }
  47.      }
  48.  }
  49.  
  50. stock HeapSort (array [], n = sizeof (array))
  51. {
  52.     new i;
  53.     for (i = n / 2; i > 0; --i)
  54.     SiftDown (array, i, n);
  55.  
  56.     for (i = n; i > 1; --i)
  57.     {
  58.         Swap (array [i - 1], array [0]);
  59.         SiftDown (array, 1, i - 1);
  60.     }
  61. }
  62. stock quickSort(array[], left, right)
  63. {
  64.     new
  65.     tempLeft = left,
  66.     tempRight = right,
  67.     pivot = array[(left + right) / 2],
  68.     tempVar
  69.     ;
  70.     while(tempLeft <= tempRight)
  71.     {
  72.         while(array[tempLeft] < pivot) tempLeft++;
  73.         while(array[tempRight] > pivot) tempRight--;
  74.  
  75.         if(tempLeft <= tempRight)
  76.         {
  77.             tempVar = array[tempLeft], array[tempLeft] = array[tempRight], array[tempRight] = tempVar;
  78.             tempLeft++, tempRight--;
  79.         }
  80.     }
  81.     if(left < tempRight) quickSort(array, left, tempRight);
  82.     if(tempLeft < right) quickSort(array, tempLeft, right);
  83. }
  84. static stock SiftDown (array [], i, m)
  85. {
  86.     new j;
  87.     while (2 * i <= m)
  88.     {
  89.         j = 2 * i;
  90.         if (j < m && array [j - 1] < array [j])
  91.         ++j;
  92.  
  93.         if (array [i - 1] < array [j - 1])
  94.         {
  95.             Swap (array [i - 1], array [j - 1]);
  96.             i = j;
  97.         }
  98.         else
  99.         i = m;
  100.     }
  101. }
  102.  
  103. static stock Swap (&a, &b)
  104. {
  105.     new s;
  106.     s = a;
  107.     a = b;
  108.     b = s;
  109. }
  110.  
  111. main()
  112. {
  113.  
  114.     //START_BENCH( 1000 );
  115.  
  116.  
  117.  
  118.  
  119.  
  120.     for(new zf;zf < sizeof(lilarray);zf++)
  121.     {
  122.         lilarray[zf]=random(100);
  123.         printf("%d",lilarray[zf]);
  124.  
  125.     }
  126.     for(new zf;zf < sizeof(lilarray);zf++)
  127.     {
  128.         heaparray[zf]=lilarray[zf];
  129.  
  130.  
  131.     }
  132.         for(new zf;zf < sizeof(lilarray);zf++)
  133.     {
  134.         bubblearray[zf]=lilarray[zf];
  135.  
  136.  
  137.     }
  138.  
  139.     print(" ");
  140.     START_BENCH( 1000 );
  141.  
  142.     quickSort(lilarray, 0, sizeof(lilarray) - 1);
  143.  
  144.  
  145.     FINISH_BENCH( "quicksort" );
  146.  
  147.     START_BENCH( 1000 );
  148.  
  149.  
  150.     HeapSort(heaparray);
  151.  
  152.  
  153.     FINISH_BENCH( "heapsort" );
  154.    
  155.     START_BENCH( 1000 );
  156.  
  157.  
  158.     BubbleSort(bubblearray);
  159.  
  160.  
  161.     FINISH_BENCH( "bubblesort" );
  162.    
  163.     printf("%d",1<<1);
  164.     printf("%d",2<<1);
  165.     printf("%d",4<<1);
  166.     printf("%d",8<<1);
  167. }
  168. public OnGameModeInit()
  169. {
  170.  
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement