Psalm 51 was written first, In it is recorded David's confession of his sin against God with Bathsheba and later making sure her husband Uriah was killed in battle in…
After being confronted by the prophet Nathan, David came to the end of himself and repented of the sins of adultery and murder.