CGTalk Qsort Challenge
 07-12-2013, 05:23 PM #16 DaveWortley   portfolio David Wortley Technical Director London, United Kingdom   Join Date: Dec 2004 Posts: 1,891 100 1000 10000 Yeah I'm not going to try 100000 OK sortCorrect() sortIncorrect() correct iterations:399 time:1 memory:3392L #(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...) incorrect iterations:1165 time:3 memory:38096L #(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...) OK sortCorrect() sortIncorrect() correct iterations:3332 time:10 memory:159016L #(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...) incorrect iterations:86508 time:157 memory:4818376L #(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...) OK sortCorrect() sortIncorrect() correct iterations:36699 time:74 memory:2023344L #(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...) incorrect iterations:8388633 time:16438 memory:23001380L #(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...) So by having my function as... Code: ``` Case of ( ((v1[2]) == (v2[2]) and (v1[1] < v2[1])) : -1 ((v1[2]) == (v2[2]) and (v1[1] > v2[1])) : 1 ((v1[2]) < (v2[2])) : -1 ((v1[2]) > (v2[2])) : 1 default: 0 )``` I Should be ok? __________________ Maxscript Made Easy... http://davewortley.wordpress.com/ share quote
07-12-2013, 05:31 PM   #17
PolyTools3D
Expert

portfolio
Jorge Rodríguez
Argentina

Join Date: Jun 2013
Posts: 1,225
Quote:
 Originally Posted by denisT this is the base c# sort function (sort by first and by next) : Code: ``` public static object[][] Sort(object[][] array) { Array.Sort(array, delegate(object[] x, object[] y) { int result = (x[0] as IComparable).CompareTo(y[0]); return (result != 0) ? result : (x[1] as IComparable).CompareTo(y[1]); }); return array; } ``` array of several types (integer, float, string) max automatically converts to object[]

Thank you Denis!
And for Point2/Point3 they should be converted to array first?

#( #([1,1,1],[2,2,2],[3,3,3]), #([4,4,4],[5,5,5],[6,6,6]) )
#( #(#(1,1,1),#(2,2,2),#(3,3,3)), #(#(4,4,4),#(5,5,5),#(6,6,6)) )
__________________
Jorge Rodríguez
PolyTools3D

07-12-2013, 05:40 PM   #18
denisT
MAX Doctor

portfolio
Denis Trofimov
CA, USA

Join Date: Jul 2009
Posts: 9,787
Quote:
 Originally Posted by PolyTools3D Thank you Denis! And for Point2/Point3 they should be converted to array first? #( #([1,1,1],[2,2,2],[3,3,3]), #([4,4,4],[5,5,5],[6,6,6]) ) #( #(#(1,1,1),#(2,2,2),#(3,3,3)), #(#(4,4,4),#(5,5,5),#(6,6,6)) )

true... only three base mxs types max aromatically converts to .net array (object)... also arrays of .net objects (classes)

07-12-2013, 06:13 PM   #19
denisT
MAX Doctor

portfolio
Denis Trofimov
CA, USA

Join Date: Jul 2009
Posts: 9,787
Quote:
 Originally Posted by DaveWortley So by having my function as... Code: ``` Case of ( ((v1[2]) == (v2[2]) and (v1[1] < v2[1])) : -1 ((v1[2]) == (v2[2]) and (v1[1] > v2[1])) : 1 ((v1[2]) < (v2[2])) : -1 ((v1[2]) > (v2[2])) : 1 default: 0 ) ``` I Should be ok?

that's OK. but if you would count the worse case you see that it needs make 6 comparisons.
why? if you can do the same for only 4?
Code:
```
fn sortByIndex v1 v2 = if (v1[2] < v2[2]) then -1 else if (v1[2] > v2[2]) then 1 else if (v1[1] < v2[1]) then -1 else if (v1[1] > v2[1]) then 1 else 0```

 07-12-2013, 06:13 PM #20 CGTalk Moderation Expert   Join Date: Sep 2003 Posts: 1,066,478 Thread automatically closed This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum. __________________ CGTalk Policy/Legalities Note that as CGTalk Members, you agree to the terms and conditions of using this website. share quote