# using qsort to sort one array based on another array?

THREAD CLOSED
 Thread Tools Display Modes
 12 December 2010 theTavis New Member   portfolio Bill Tavis USA using qsort to sort one array based on another array? Hi, I am trying to optimize a function using qsort to sort a very large array of Point3 world coordinates. I found it was faster to precompute the values and store them in a parallel array with a for-collect loop. My question is: using qsort, how do I reference these values to sort the Point3 array directly? My current work-around requires two additional for-collect loops as in this example: ``````valueArr = #() -- declare here so it can be seen by functions fn sortFn a b = if valueArr[a] > valueArr[b] then 1 else -1 fn sortPoints pointArr = ( indexArr = for i = 1 to pointArr.count collect i -- collect the indices, these are what we sort valueArr = for p in pointArr collect (cos p.x + sin p.y) -- compute a value to sort by qsort indexArr sortFn -- sort the indices for i = 1 to pointArr.count collect pointArr[indexArr[i]] -- return the points sorted by index )`````` This works fine, but for large point counts it would be faster without the first and last for-collect loops. Is it possible to do something like this: ``````valueArr = #() -- declare here so it can be seen by functions -- here's the part I don't know how to do: getting the index of a or b fn sortFn a b = if valueArr[(index of a)] > valueArr[(index of b)] then 1 else -1 fn sortPoints pointArr = ( valueArr = for p in pArr collect (cos p.x + sin p.y) -- compute some value to sort by qsort pointArr sortFn -- sort the actual point array )`````` any help is appreciated. Thanks! share quote
 12 December 2010 Klunk Lord of the posts   portfolio Klunk United Kingdom the sort will be faster as it only has to swap integer values and not point3 values, I guessing about 3 times faster. If you need to actually sort the array then at some stage you are always going to have to do the point3 "swaps". You can always access the point3 array via the index array though e.g. verts[indices[i]] not very neat and probably quite slow. share quote
 12 December 2010 denisT MAX Doctor   portfolio Denis Trofimov CA, USA I think the fastest way to sort this kind of data will be: `````` -- make a buffer, for example: seed 0 data = for k=1 to 100000 collect ( d = random [-180,-180] [180,180] cs = (cos d.x) + (sin d.y) #(d, cs) ) -- sort by sincos fn sortByN2 a b = if a[2] < b[2] then -1 else if a[2] > b[2] then 1 else 0 qsort data sortByN2 final = for d in data collect d[1] `````` Last edited by denisT : 12 December 2010 at 12:39 AM. share quote
 12 December 2010 theTavis New Member   portfolio Bill Tavis USA thanks DenisT! Pairing the sort value along with the world coordinates is a great solution, it saves me from having an extra array floating around outside the functions, and let's me eliminate one of the for-collect loops. My code is cleaner now, and a touch faster too. Thanks again! share quote
 12 December 2010 denisT MAX Doctor   portfolio Denis Trofimov CA, USA Originally Posted by theTavis: ... it saves me from having an extra array floating around outside the functions... probably some of maxscript coders don't know that is possible to pass extra parameters to qsort function. like: `````` fn sortbybuffer a b buffer: = if buffer[a] < buffer[b] then -1 else if buffer[a] > buffer[b] then 1 else 0 qsort data sortbybuffer buffer:other_array`````` or: `````` fn power a b = pow a b fn sortwithfunction a b fun: = ( d1 = fun a b d2 = fun b a if d1 < d2 then -1 else if d1 > d2 then 1 else 0 ) qsort data sortwithfunction fun:power`````` PS. the last sample is just a sample. It's not a good idea to use any calculation inside qsort loop. The warranty of any fast sorting is the data prepared in advance . share quote
 12 December 2010 CGTalk Moderation Expert 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
 Thread Closed share thread

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off CGSociety Society of Digital Artists www.cgsociety.org Powered by vBulletinCopyright ©2000 - 2006, Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
 Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Gallery     Latest Entries     Featured 2D     Featured 3D     CG Awards     Community Awards Main     News     General Discussion     Off Topic     Recruitment         Temp Job Board Challenges     Rival Crimson X Chaos     Education     Demo Reel     Portfolio Review GROUPS WIP ADVICE     Pro Select     Art Direction and Interviews     New 2D/3D Sketchbooks 2018     WIP 2D/3D & Animation         WIP/Critique: 3D         WIP/Critique: 2D             Speedpaints & Sketchbooks         WIP/Critique: Animation     Partners and Groups         Kitbash 3D Thunderdome         Collaborative Projects Autodesk     Autodesk Maya         Maya Dynamics         Maya Rendering         Maya Character Setup         Maya Programming     Autodesk Miscellaneous         Autodesk MotionBuilder         Autodesk Mudbox         Autodesk Softimage             XSI: Programming             ICE: Interactive Creative Environment     Autodesk 3ds max         3dsMax Tutorials & Tips         3dsMax Resources         3dsMax SDK and MaxScript         Plugins & Add-ons         Particle Flow     Mudbox Featured Gallery ZBrush     Featured Zbrush Gallery     Pixologic ZBrush     Keyshot     Marvelous Designer Texturing     Allegorithmic Substance         Substance Designer         Substance Painter         Substance Featured Gallery     Texturing XYZ     Mari     Marmoset Toolbag Software     Maxon Cinema 4D         Cinema 4D Resources     Side Effects Houdini     Compositing Software         The Foundry Nuke         Adobe After Effects         Autodesk Effects and Compositing         Eyeon Software Digital Fusion     Additional Software         Lightwave 3D             LW Resources             LW Tutorials         The Foundry Modo             The Foundry Time Travel Challenge         E-on Software Vue         Photoshop / Painter         Blender         3DCoat Techniques     Art Techniques and Theories     Digital Matte Painting         Digital Matte Painting Mini-Challenge     Modeling         Modeling Challenge             Member Model Collection     Texturing and Surfacing     Character Rigging     Animation     Lighting and Shaders         Lighting Challenges     Compositing and Editing     Anatomy and Figurative Art         SPOTLIGHT: Best of the Artistic Anatomy and Figurative Art Forum         Tutorials and Workshops         Personal Anatomy & Sketchbook Threads         References, Resources, and Supplies Technical     VR/AR     Master Virtual Reality with BrioVR     Workstation Builds     Game Engine     Graphics Programming     Technical and Hardware CGS Labs     Sketchbooks     Hardcore Crit     Discussions and Meet Ups
Miscellaneous

All times are GMT. The time now is 05:45 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.