That algorithm takes time O(n) to insert a value into a queue of length n. That is very slow. A better implementation uses only O(1) time per operation.