By the time of the flood, Noah was the only righteous man on earth:
Genesis 6:9 Noah was a righteous man, the only blameless person living on earth at the time, and he walked in close fellowship with God.
The book of Hebrews in the New Testament tell us that it wasn’t anything Noah did that made him righteous. It was his faith in God:
Hebrews 11:7 It was by faith that Noah built a large boat to save his family from the flood. He obeyed God, who warned him about things that had never happened before. By his faith Noah condemned the rest of the world, and he received the righteousness that comes by faith.
Noah built the boat because of his faith, not to show off or prove his faith. It wasn’t about passing some test, but a logical response to a directive from God.
It was this faith that imparted righteousness to Noah and saved him physically as well as spiritually.