function [sites] = sort_sites(sites, nsites)

gap=floor(nsites/2);
while(gap > 0)
   for i=gap:(nsites-1)
      j = i - gap + 1;
      while(j>0)
         if(sites{j}.x ~= sites{j+gap}.x)
            if(sites{j}.x < sites{j+gap}.x)
               break;
            end;
         else
            if(sites{j}.y < sites{j+gap}.y)
               break;
            end;
         end;
         tmp = sites{j};
         sites{j} = sites{j+gap};
         sites{j+gap} = tmp;
         
         j = j - gap;
      end;
   end;
   
   gap = floor(gap/2);
end;
   
%void sort_sites(site_struct sites[], int nsites)
%  {
%    int gap, i, j;
%    site_struct tmp;

%    for (gap = nsites/2; gap > 0; gap /= 2)
%      for (i = gap; i < nsites; i++)
%	for (
%	    j = i-gap; 
%	    j >= 0 && 
%	      ( sites[j].x != sites[j+gap].x ? 
%                (sites[j].x > sites[j+gap].x) : 
%                (sites[j].y > sites[j+gap].y)
%              );
%	    j -= gap
%	  ) 
%	  {
%	    tmp = sites[j]; sites[j] = sites[j+gap]; sites[j+gap] = tmp;
%	  }
%  }
