Sorting lists

About advanced scenes, and the Thyme scripting language used in Algodoo.

Sorting lists

Postby Kilinich » Fri Jun 04, 2010 7:29 am

I don't know yet where to use it :) , but it's pretty cool.
Now you can sort huge lists with no lags.

Code: Select all
Scene.my.QSort := (array, size)=>{
    size > 2 ? {
        b := math.toint(size / 2);
        low := []; lowCount := 0;
        high := []; highCount := 0;
        for(b, (i)=>{
            array(i) > array(b) ? {
                highCount = highCount + 1; high = high ++ [array(i)]
            } : {
                lowCount = lowCount + 1; low = low ++ [array(i)]
            }
        });
        for(size - b - 1, (i)=>{
            array(i + b + 1) > array(b) ? {
                highCount = highCount + 1; high = high ++ [array(i + b + 1)]
            } : {
                lowCount = lowCount + 1; low = low ++ [array(i + b + 1)]
            }
        });
        (lowCount > 0 ? Scene.my.QSort(low, lowCount) : []) ++ [array(b)] ++ (highcount > 0 ? Scene.my.QSort(high, highCount) : [])
    } : {size == 2 ? {array(0) < array(1) ? array : [array(1), array(0)]} : array}
};


Scene.my.qSort([10,23,3,1,2,55],6) produce [1,2,3,10,23,55]
Last edited by Kilinich on Fri Jun 04, 2010 7:50 am, edited 1 time in total.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Sorting lists

Postby Kilinich » Fri Jun 04, 2010 7:39 am

And little more complex version for sorting 2-dimensional tables:
key is index of key element 0= first

Code: Select all
Scene.my.QSortTable := (array, size, key)=>{
    size > 2 ? {
        b := math.toint(size / 2);
        low := [];
        lowCount := 0;
        high := [];
        highCount := 0;
        for(b, (i)=>{
            array(i)(key) > array(b)(key) ? {
                highCount = highCount + 1;
                high = high ++ [array(i)]
            } : {
                lowCount = lowCount + 1;
                low = low ++ [array(i)]
            }
        });
        for(size - b - 1, (i)=>{
            array(i + b + 1)(key) > array(b)(key) ? {
                highCount = highCount + 1;
                high = high ++ [array(i + b + 1)]
            } : {
                lowCount = lowCount + 1;
                low = low ++ [array(i + b + 1)]
            }
        });
        (lowCount > 0 ? Scene.my.QSortTable(low, lowCount, key) : []) ++ [array(b)] ++ (highcount > 0 ? Scene.my.QSortTable(high, highCount, key) : [])
    } : {size == 2 ? {array(0)(key) < array(1)(key) ? array : [array(1), array(0)]} : array}
};



Scene.my.QSortTable([[10,"Ten"],[1,"One"],[2,"Two"],[5,"Five"],[-1,"Minus one"]], 5, 0)
result is:
[[-1,"Minus one"],[1,"One"],[2,"Two"],[5,"Five"],[10,"Ten"]]
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Sorting lists

Postby Matten » Sat Jun 05, 2010 4:43 pm

It looks good, but is somebody ever going to use this?
Cave Johnson wrote:Do you know who I am? I'm the man who's gonna burn your house down! With the lemons! I'm gonna get my engineers to invent a combustible lemon that burns your house down!
User avatar
Matten
 
Posts: 435
Joined: Mon Apr 05, 2010 2:03 pm
Location: The Netherlands

Re: Sorting lists

Postby Mystery » Sat Jun 05, 2010 5:03 pm

Yep, I'll be looking into this, looks promising could save me a lot of time.
User avatar
Mystery
 
Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

Re: Sorting lists

Postby Kilinich » Sat Jun 05, 2010 6:36 pm

Matten wrote:It looks good, but is somebody ever going to use this?

I am :lol:
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 13 guests