Don't sort documents twice in SortFieldWriter#Refill

The doc_ids are already sorted in S_lazy_init_sorted_ids. We only have
to make sure that S_lazy_init_sorted_ids uses the doc_id as secondary
sort key.
Make SortFieldWriter#Refill obey the memory limit

The old logic was broken.
Initialize SortFieldWriter#run_tick to 1

Make sure we never use a run_tick of 0.
Free sorted_ids in SortFieldWriter a little earlier
Use counting sort to sort doc_ids in SortFieldWriter#Refill

Since we already have the ordinals for each doc_id, we can use a
counting sort. This uses a temporary array of size run_cardinality but
runs in linear time.
Convert t/221-sort_writer.t to C

Original commit by Nick Wellnhofer, amended by Marvin Humphrey to
replace "cnick" with "nickname".
Port t/015-sort_external.t to C

Original commit by Nick Wellnhofer, updated by Marvin Humphrey for
changes to the SortExternal API.
Port t/028-sortexrun.t to C

Original commit by Nick Wellnhofer, amended by Marvin Humphrey for
changes to the SortExternal API.