viewing paste topic/14817- quick/comb/counting | Athena

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
poring_w01,90,100,5 script  quick sort  1_F_MARIA,{
    callfunc "test_sort", "quick";
}
poring_w01,92,100,5 script  counting sort   1_F_MARIA,{
    callfunc "test_sort", "counting";
}
poring_w01,94,100,5 script  comb sort   1_F_MARIA,{
    callfunc "test_sort", "comb";
}
 
function    script  test_sort   {
    freeloop true;
    .@total = 1000;
    for ( .@i = 0; .@i < .@total; ++.@i )
        .@array[.@i] = rand(50);
    .@start = gettimetick(0);
    callfunc getarg(0), .@array;
    .@end = gettimetick(0);
    for ( .@i = 0; .@i < .@total; ++.@i )
        .@print$[.@i] = .@array[ .@i ];
    debugmes implode( .@print$, "," );
    dispbottom "time used -> "+( .@end - .@start )+" mili-seconds";
    end;
}
 
function    script  array_shuffle   {
    .@size = getarraysize(getarg(0));
    for (.@i = .@size - 1; .@i >= 1; --.@i)
        swap(getelementofarray(getarg(0), rand(0, .@i)), getelementofarray(getarg(0), .@i));
    return true;
}
 
//  credit to keyworld
function    script  counting    {
    .@total = .@size = getarraysize( getarg(0) );
    copyarray .@arr, getarg(0), .@size;
    for ( .@i = 0; .@i < .@size; ++.@i )
        ++.@tmp[.@arr[.@i]];
    for ( ; .@size; --.@size ) {
        .@index = getarraysize(.@tmp) -1;
        .@output[.@size-1] = .@index;
        --.@tmp[.@index];
    }
    copyarray getarg(0), .@output, .@total;
    return;
}
 
//  credit to meko
function    script  quick   {
    callsub(S_Quicksort, getarg(0), getarrayindex(getarg(0)), getarraysize(getarg(0)) - 1);
    return true;
 
S_Quicksort:
    if (getarg(1) < getarg(2)) {
        .@p = callsub(S_Partition, getarg(0), getarg(1), getarg(2));
        callsub(S_Quicksort, getarg(0), getarg(1), .@p - 1);
        callsub(S_Quicksort, getarg(0), .@p + 1, getarg(2));
    }
    return true;
 
S_Partition:
    .@i = getarg(1) - 1;
    for (.@j = getarg(1); .@j <= getarg(2) - 1; ++.@j)
        if (getelementofarray(getarg(0), .@j) < getelementofarray(getarg(0), getarg(2)))
            swap(getelementofarray(getarg(0), ++.@i), getelementofarray(getarg(0), .@j));
    swap(getelementofarray(getarg(0), ++.@i), getelementofarray(getarg(0), getarg(2)));
    return .@i;
}
 
//  credit to haru & keyworld
function    script  comb    {
    .@size  = getarraysize( getarg(0) );
    .@gap   = .@size;
    do {
        .@gap   = .@gap > 1 ? .@gap * 10 / 13 : .@gap;
        for ( .@i = .@gap; .@i < .@size; ++.@i )
            if ( getelementofarray( getarg(0), .@i - .@gap ) > getelementofarray( getarg(0), .@i ) )
                swap getelementofarray( getarg(0), .@i - .@gap ), getelementofarray( getarg(0), .@i );
    } while ( .@gap > 1 );
    return;
}
 
function    script  printdispbottom {
    .@size = getarraysize( getarg(1) );
    for ( .@i = 0; .@i < .@size; ++.@i )
        .@print$ += getelementofarray( getarg(1), .@i )+",";
    dispbottom getarg(0) +" = "+ .@print$;
    return;
}
 
poring_w01,105,100,5    script  debug_sort  1_F_MARIA,{
    dispbottom " ==== "+ strnpcinfo(NPC_NAME) +" ===";
    setarray .@a, 0,1,2,3,4,5,6,7,8,9,
        1,2,5,6,3,8,9,4,6,3,5,6,8,3,5;
    printdispbottom "original", .@a;
    array_shuffle .@a;
    printdispbottom " shuffle", .@a;
    quick .@a;
    printdispbottom "     sort", .@a;
    end;
}
 
poring_w01,105,105,5    script  test string 1_F_MARIA,{
    dispbottom ( "test" == "test" ); // return 1
    dispbottom ( "123" > "321" ); // return 0
    dispbottom ( "asdf" < "qwer" ); // return 1
    end;
}
Viewed 1078 times, submitted by AnnieRuru.