sorted the input in final loop so the output comes out sorted