John Swinney's party finished comfortably ahead of its rivals but failed to secure an overall majority.