I've been using insertion sort a lot for my current project, primarily because it's really simple to implement. I thought I might share it here for those who haven't heard of it. It is really simple to use, and good for small linked lists like all of us have in our roguelikes. Anyway, here it is in C++:
void Sort()
{
int i;
int j;
vector<string> Names;
string Name;
for(i=0;i<Names.size();i++)
{
Name=Names[i];
for(j=i;j>-1;j--)
{
if(Names[i]>Names[j])//Switch this for ascending versus descending.
{
Names.erase(Names.begin()+i);
Names.insert(Names.begin()+j+1,Name);
break;
}
else if(j==0)
{
Names.erase(Names.begin()+i);
Names.insert(Names.begin(),Name);
break;
}
}
}
}
Viola! Two loops, and the list is 100% sorted. Here's how it works:
The list is walked from left (0) to right (size of list). At each location, the list to the left is assumed to be sorted, and to the right is assumed to be unsorted. For each element, we take it from it's current unsorted position, and walk backward over the sorted list to find the element's correct location. We then insert the element in it's correct location on the left, and continue our journey to the right.
Single pass, requires no additional list storage, runs in O(n) in already sorted arrays, and easy to implement! Enjoy.
PS: Don't use it on lists of 1000+, unless they're already partially ordered.