Here's an example of quicksort in DBPro:
global arrsize as integer
arrsize=10
dim arr(arrsize) as integer
for n=0 to arrsize
arr(n)=rnd(arrsize)
next n
quicksort(0,arrsize)
printArr()
wait key
end
function printArr()
x=0
y=0
w=text width(str$(arrsize))
for n=0 to arrsize
center text x+w/2,y,str$(arr(n))
y=y+16
if y+16>screen height()
y=0
x=x+w
endif
next n
endfunction
function quicksort(first, last)
if first>=last then exitfunction
element=(first+last)/2
elementval=arr(element)
leftct=first+1
rightct=last
arr(element)=arr(first) //swap
arr(first)=elementval
while leftct<rightct
while arr(leftct)<=elementval and leftct<rightct
inc leftct
endwhile
while arr(rightct)>elementval and leftct<rightct
dec rightct
endwhile
tmp=arr(leftct) //swap
arr(leftct)=arr(rightct)
arr(rightct)=tmp
cls
printarrhighlight(first,rgb(0,255,0),leftct,rgb(255,0,0),rightct,rgb(0,0,255))
wait key
endwhile
if arr(leftct)>elementval
arr(first)=arr(leftct-1) //swap
arr(leftct-1)=elementval
quicksort(first,leftct-2) //recursive step
quicksort(leftct,last)
else
arr(first)=arr(leftct) //swap
arr(leftct)=elementval
quicksort(first,leftct-1) //recursive step
quicksort(leftct+1,last)
endif
endfunction
I just posted a more commented version in the code snippets board if you're wondering how the algorithm works (though wikipedia would be better than my comments probably), but basically recursionn is the most straightforward way to think about it.
Here's an implementation (in C++) of a quadtree:
//.h file:
class QuadNode
{
glm::dvec2 tl; //top left of the cell
glm::dvec2 br; //bottom right position of the cell
QuadNode *subnodes[2][2]; //recursive/nested members
glm::dvec2 *val; //each cell contains ONE pointer to a point.
void createSubNodes(); //private
public:
QuadNode(glm::dvec2 tl, glm::dvec2 br); //constructor
~QuadNode(); //destructor
void addPointMass(PointMass *arg); //add a mass to the node
};
//the : thing is equivalent to "this->tl=tl; this->br=br; this->val=NULL;".
QuadNode::QuadNode(glm::dvec2 tl, glm::dvec2 br) : tl(tl),br(br),val(NULL)
{
//make sure the sub-pointers are zero and
for(int n=0;n<2;n++)
for(int m=0;m<2;m++)
subnodes[n][m]=NULL;
}
QuadNode::~QuadNode()
{
//deletes all subnodes - this destructor is recursive.
for(int n=0;n<2;n++)
for(int m=0;m<2;m++)
if(subnodes[n][m])
delete subnodes[n][m];
}
void QuadNode::createSubNodes()
{
//creates four subnodes and gives them appropriate bounds.
glm::dvec2 mid=(tl+br)*.5;
glm::dvec2 midleft=glm::dvec2(tl.x,mid.y);
glm::dvec2 midtop=glm::dvec2(mid.x,tl.y);
glm::dvec2 midright=glm::dvec2(br.x,mid.y);
glm::dvec2 midbottom=glm::dvec2(mid.x,br.y);
subnodes[0][0]=new QuadNode(tl,mid);
subnodes[0][1]=new QuadNode(midtop,midright);
subnodes[1][0]=new QuadNode(midleft,midbottom);
subnodes[1][1]=new QuadNode(mid,br);
}
void QuadNode::addPoint(glm::dvec2 *arg)
{
if(val==NULL) //if the node doesn't currently contain a point
{
if(subnodes[0][0]==NULL) //no point & no subnodes. "base case".
{
val=arg; //assign the point
} else { //if the current node has subnodes, add the arg to the appropriate subnode.
//this is a recursive step.
glm::dvec2 mid=(tl+br)*.5;
int x=arg->x>mid.x;
int y=arg->y<mid.y;
subnodes[y][x]->addPoint(arg);
}
}
else { //there's currently a point in the current node AND we want to add another one.
if(*val==*arg) //if the points point to the same spot, no amount of subdivision will save us
return; //so give up
glm::dvec2 *valtemp=val;
val=NULL; //remove the current value
createSubNodes(); //subdivide
addPoint(valtemp); //re-add the nodes. (recursive)
addPoint(arg);
}
}
That code should compile (i haven't tried it, I modified it a bit removing the application-specific code I was using it for). To see a quadtree in action, go to
http://mathandcode.com/programs/javagrav/ and click "start" and "show quadtree".
There's also this DBPro application but it's more complicated than the C++ version (in my mind at least) because it has to be procedural not object oriented
http://forum.thegamecreators.com/?m=forum_view&t=186678&b=6
If you want examples of some hard recursion problems (whose solutions are almost always elegant I might add!) check out codingbat:
http://codingbat.com/java/Recursion-2
If you know java (VERY similar to c++ here because there's just one function) you can run the code on that site and have it test it for you, otherwise you'll have to code it up in some other language and verify for yourself that it works. I think there's also a python version.