An efficient algorithm for choosing the degree of a polynomial to approximate discrete nonoscillatory data