Knogle icon

Sort

Knogle | PRO | 02/26/17 08:14:13 PM UTC | 0 ⭐ | 469 👁️ | Never ⏰ | []
C |

3.15 KB

|

None

|

0 👍

/

0 👎

 
#include <a_samp>
#include <hex>
//new
//  lilarray[] = { -541, 54, 689, 12, 3, 0, 3, 55, 66, -541, 5468484, -564, 1554, 1656 }
//;
new lilarray[1024];
new bigarray[sizeof(lilarray)];
new heaparray[sizeof(lilarray)];
new bubblearray[sizeof(lilarray)];
 
#pragma tabsize 0
#define START_BENCH(%0); {new __a=%0,__b=0,__c,__d=GetTickCount(),__e=1;do{}\
        while(__d==GetTickCount());__c=GetTickCount();__d=__c;while(__c-__d<__a||\
        __e){if(__e){if(__c-__d>=__a){__e=0;__c=GetTickCount();do{}while(__c==\
                    GetTickCount());__c=GetTickCount();__d=__c;__b=0;}}{
 
            #define FINISH_BENCH(%0); }__b++;__c=GetTickCount();}printf(" Bench for "\
        %0": executes, by average, %.2f times/ms.",floatdiv(__b,__a));}
        
        
 
 
stock Float:frandom(Float:max, Float:min = 0.0, dp = 4)
{
    new
    Float:mul = floatpower(10.0, dp),
    imin = floatround(min * mul),
    imax = floatround(max * mul);
    return float(random(imax - imin) + imin) / mul;
}
stock BubbleSort(array[],length = sizeof(array))
 {
     new i, j;
     for (i = 0; i < length - 1; ++i)
     {
 
        for (j = 0; j < length - i - 1; ++j)
        {
            if (array[j] > array[j + 1])
            {
                new tmp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = tmp;
            }
        }
     }
 }
 
stock HeapSort (array [], n = sizeof (array))
{
    new i;
    for (i = n / 2; i > 0; --i)
    SiftDown (array, i, n);
 
    for (i = n; i > 1; --i)
    {
        Swap (array [i - 1], array [0]);
        SiftDown (array, 1, i - 1);
    }
}
stock quickSort(array[], left, right)
{
    new
    tempLeft = left,
    tempRight = right,
    pivot = array[(left + right) / 2],
    tempVar
    ;
    while(tempLeft <= tempRight)
    {
        while(array[tempLeft] < pivot) tempLeft++;
        while(array[tempRight] > pivot) tempRight--;
 
        if(tempLeft <= tempRight)
        {
            tempVar = array[tempLeft], array[tempLeft] = array[tempRight], array[tempRight] = tempVar;
            tempLeft++, tempRight--;
        }
    }
    if(left < tempRight) quickSort(array, left, tempRight);
    if(tempLeft < right) quickSort(array, tempLeft, right);
}
static stock SiftDown (array [], i, m)
{
    new j;
    while (2 * i <= m)
    {
        j = 2 * i;
        if (j < m && array [j - 1] < array [j])
        ++j;
 
        if (array [i - 1] < array [j - 1])
        {
            Swap (array [i - 1], array [j - 1]);
            i = j;
        }
        else
        i = m;
    }
}
 
static stock Swap (&a, &b)
{
    new s;
    s = a;
    a = b;
    b = s;
}
 
main()
{
 
    //START_BENCH( 1000 );
 
 
 
 
 
    for(new zf;zf < sizeof(lilarray);zf++)
    {
        lilarray[zf]=random(100);
        printf("%d",lilarray[zf]);
 
    }
    for(new zf;zf < sizeof(lilarray);zf++)
    {
        heaparray[zf]=lilarray[zf];
 
 
    }
        for(new zf;zf < sizeof(lilarray);zf++)
    {
        bubblearray[zf]=lilarray[zf];
 
 
    }
 
    print(" ");
    START_BENCH( 1000 );
 
    quickSort(lilarray, 0, sizeof(lilarray) - 1);
 
 
    FINISH_BENCH( "quicksort" );
 
    START_BENCH( 1000 );
 
 
    HeapSort(heaparray);
 
 
    FINISH_BENCH( "heapsort" );
    
    START_BENCH( 1000 );
 
 
    BubbleSort(bubblearray);
 
 
    FINISH_BENCH( "bubblesort" );
    
    printf("%d",1<<1);
    printf("%d",2<<1);
    printf("%d",4<<1);
    printf("%d",8<<1);
}
public OnGameModeInit()
{
 
 
}

Comments