kth, a useful algorithm