viewing paste /topic/4321- haru merge sort | 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
prontera,152,188,5  script  test merge sort 100,{
    .@total = 10;   
    for ( .@i = 0; .@i < .@total; .@i++ )
        .@array[.@i] = rand(100);
    .@start = gettimetick(0);
    freeloop 1;
    callfunc "merge_sort", .@array, .@output, .@total;
    .@end = gettimetick(0);
    for ( .@i = 0; .@i < .@total; .@i++ )
        dispbottom .@array[ .@output[.@i] ] +"";
    dispbottom "time used -> "+( .@end - .@start )+" mili-seconds";
    end;
}
 
prontera,155,188,5  script  getitemname2 ID sorted  100,{
    freeloop 1;
    getinventorylist;
    dispbottom "Merge sort === Total items : "+ @inventorylist_count +" ===";
    callfunc "merge_sort", @inventorylist_id, .@output, @inventorylist_count;
    .@i = 0;
    while ( .@i < @inventorylist_count ) {
        .@itemname$ = callfunc("getitemname2", @inventorylist_id[ .@output[.@i] ], @inventorylist_identify[ .@output[.@i] ], @inventorylist_refine[ .@output[.@i] ], @inventorylist_attribute[ .@output[.@i] ], @inventorylist_card1[ .@output[.@i] ], @inventorylist_card2[ .@output[.@i] ], @inventorylist_card3[ .@output[.@i] ], @inventorylist_card4[ .@output[.@i] ]);
        dispbottom "("+ @inventorylist_id[ .@output[.@i] ] +") "+ .@itemname$ +": "+ @inventorylist_amount[ .@output[.@i] ] +" ea.";
        .@i++;
    }
    end;
}
 
//  callfunc "merge_sort", <input array>, <output index>, <total index>;
// Credits to Haru -> http://hercules.ws/board/topic/4321-
function    script  merge_sort  {
    .@size = getarg(2);
    copyarray .@arr, getarg(0), .@size;
    while ( .@i < .@size ) {
        set getelementofarray(getarg(1), .@i), .@i;
        .@i++;
    }
    .@width = 1;
    while ( .@width < .@size ) {
        .@i = 0;
        while ( .@i < .@size ) {
            .@left = .@i;
            .@middle = .@i + .@width;
            .@right = .@i + 2*.@width;
            if (.@size < .@middle) .@middle = .@size;
            if (.@size < .@right) .@right = .@size;
            .@i0 = .@left;
            .@i1 = .@middle;
            .@j = .@left;
            while ( .@j < .@right ) {
                if (.@i0 < .@middle && (.@i1 >= .@right || .@arr[.@i0] <= .@arr[.@i1])) {
                    .@tmp_array[.@j] = .@arr[.@i0];
                    .@tmp_array2[.@j] = getelementofarray(getarg(1), .@i0);
                    .@i0++;
                } else {
                    .@tmp_array[.@j] = .@arr[.@i1];
                    .@tmp_array2[.@j] = getelementofarray(getarg(1), .@i1);
                    .@i1++;
                }
                .@j++;
            }
            .@i += 2 * .@width;
        }
        .@width *= 2;
        copyarray .@arr, .@tmp_array, .@size;
        copyarray getarg(1), .@tmp_array2, .@size;
    }
    return;
}
Viewed 1343 times, submitted by AnnieRuru.