<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>FAST array sorting ? array structure order problems</title>
		<link>http://www.allegro.cc/forums/view/543554</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 16 Nov 2005 22:47:59 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I plan to make my new game and here is a problem I have to solve.</p><p>I always put all monsters and objects into one array. In the main cycle the game calls &quot;do_AI(int monster)&quot;, where &quot;monster&quot; is the index to this array and this function does anything that is needed for the specific monster.</p><p>And here goes my problem - I plan to make an isometric game, so i need to draw the monsters that are behind first and last the monsters that are in front.<br />So I need really quick to find what monsters to draw in what order <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>Anybody can help? I&#39;d like not to use classical quicksort (etc) to sort the y value <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>Is there a way to solve this with another structure as array ? (I mean to solve it faster?)</p><p>Thanx for any comment <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Pavel Hilser)</author>
		<pubDate>Tue, 08 Nov 2005 23:13:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Maybe a linked list where you add the monsters sorted instead. Or a list of pointers to your array. You sort the list, and use the pointers to know to which monster each makes use.</p><p>In the worst case, just use the qsort function to sort the array. It should be faster than what you think.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Tue, 08 Nov 2005 23:22:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Unless you have about 10k items in that array using regular array with qsort will not cause any speed problems unless you do something stupid of cource <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Tue, 08 Nov 2005 23:27:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I would use some sort of priority queue.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kentl)</author>
		<pubDate>Tue, 08 Nov 2005 23:33:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Also if your array is mostly sorted something like bubblesort can be much faster than qsort <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Tue, 08 Nov 2005 23:36:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Is there a way to solve this with another structure as array ? (I mean to solve it faster?)
</p></div></div><p>
Having an array of indices and sorting that should already be faster than sorting the array itself (at least if it&#39;s an array of objects larger than 4 bytes in size).</p><p>That said, I do things slightly differently: I construct a linked list in my main draw loop using insertion sort. That sort takes care of the drawing order (among other things) and I just draw everything in the queue in order once the list is build. Works pretty well and is quite flexible.</p><p>My original reason for doing this, by the way, is that I have several types of objects on the screen with their own peculiarities (player character, monsters, treasure, particles, projectiles...). I can either make them all the same type and put them in the same array (which I don&#39;t want to do) or do it like this.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 09 Nov 2005 00:04:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The bigger your objects are, the more effective is a linked list in comparison to an array.<br />But I believe you don&#39;t have to care about speed issues in that case. The drawing does  mostly need the most performance.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Simon Parzer)</author>
		<pubDate>Wed, 09 Nov 2005 00:46:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>well, i suppose you are right, linked list and sort it ..</p><p>(my array has about 100 bytes so i don&#39;t have to move all of this.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Pavel Hilser)</author>
		<pubDate>Wed, 09 Nov 2005 00:55:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Just to clarify, a list implies order. So, you insert elements in the correct order instead of inserting them anywhere, then ordering them <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Wed, 09 Nov 2005 01:04:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>well, that&#39;s a good idea ...</p><p>cause inserting to drawing is about 1 to 99 percent, so in inserting I could easy handle where to put them..</p><p>BUT - my objects are moving, so if I insert it somewhere, I have to move it to another place in my array, as it moves <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>I thing I&#39;ll try to do my array, array of pointers and then simply sort the array when the level is redrawn.<br />(and take care that the sort is as fast as possible)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Pavel Hilser)</author>
		<pubDate>Wed, 09 Nov 2005 02:00:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Make it work first, optimize it later. If you design things in a good way you will be able to change the way it sorts later on, and get it working soon.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kentl)</author>
		<pubDate>Wed, 09 Nov 2005 13:33:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, I&#39;m not afraid that this would not work, I&#39;ve done this hundred times before <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /> (in my every game is this monster-array), but the only thing different is the need to have the array sorted <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>I&#39;ve done some different sorting algorytms before and know how they work, but I didn&#39;t realized the use of a linked array to another array <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Pavel Hilser)</author>
		<pubDate>Wed, 09 Nov 2005 14:42:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Another way to do it would be to put a pointer to the actor on the tile on each tile. So when you want to draw stuff, iterate through each tile and draw the tile-sprite and then anything on it (actor). This is easiest to implement if you have &quot;grid-based&quot; movements though. Otherwise you would need to make some evil offset value that changes as the actor moevs around and then at a certain point you move the actor &quot;move&quot; to another tile.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Wed, 09 Nov 2005 15:12:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Or you can just use the might % operator (or even &amp; for power-of-2-sized tiles) to determine the current tile for the actor each time it move.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 11 Nov 2005 20:45:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>We have recently went over several sorting routines in my C++ class. Most of the time, the teacher just showed us java applications for examples instead of programming everything out in C++.</p><p>You might find these websites pretty useful. All the work is done for you, so you just get a quick overview of how each sorting method works.</p><p>Links:<br /><a href="http://www.cs.pitt.edu/~kirk/cs1501/animations/Sort2.html">http://www.cs.pitt.edu/~kirk/cs1501/animations/Sort2.html</a><br /><a href="http://www.cs.rit.edu/~atk/Java/Sorting/sorting.html">http://www.cs.rit.edu/~atk/Java/Sorting/sorting.html</a><br /><a href="http://max.cs.kzoo.edu/~abrady/java/sorting/InsertionSort.html">http://max.cs.kzoo.edu/~abrady/java/sorting/InsertionSort.html</a></p><p>But heres an idea...how about only a partial qsort? That is, instead of compleetly resorting the array, or vector, or whatever it is you want to use each logic step, only sort a small chunk of it. It would prevent the game from getting too slow with a very large number of objects, and who cares if one object breifly appears in front of some other object that it&#39;s not supposed to for only 1 frame as they move past each other. Once they are sorted, it&#39;s not going back...so it shouldn&#39;t cause any flicker. In special cases, you might wish to call a full resort (such as just loading a new room or map). Also, if objects will not just be jumping out everywhere randomly, you will want to choose a sorting algorithm that performs faster as things are more sorted.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elverion)</author>
		<pubDate>Wed, 16 Nov 2005 22:47:59 +0000</pubDate>
	</item>
</rss>
