Sorting algorithms, counting algorithm