sorting and bfs