sortrows() is based on quicksort.
quicksort() does not require that the comparison operation be literally x(idx1) < x(idx2) : quicksort as an algorithm just says that at a particular point, you invoke a comparison function that tells you the relative order of two entries given their index. The comparison operator can be, for example,
temp = sign(x(idx1, :) - x(idx2, :)) .* sort_direction_hints;
if ~any(temp)
sort_flag = 0;
else
sort_flag = temp(find(temp),1);
end
sort_flag of -1 means that row idx1 is "before" row idx2. sort_flag of 0 means that they are the same for sorting purposes. sort_flag if +1 means that row idx1 is "after" row idx2.
sort_direction_hints is a vector of values, one per column. -1 indicates descending sort; 0 indicates column to be ignored for sorting; +1 indicates ascending sort.
The construction of temp takes constant time proportional to the number of columns. The ~any(temp) could potentially take variable time that is at most proportional to the number of columns. The test could be rewritten to be constant time proportional to the number of columns.
The find() takes variable time that is at most proportional to the number of columns (and could be rewritten to constant time proportional to the number of columns.
The overall result is variable but proportional to the number of columns.
Ameer's
would become
where m is the number of columns. This is worst case; an actual dedicated implementation could reduce the constants of proportionality .