The most primitive branches of bony fish all have lungs, while swim bladders are found only in the teleost branch. The simplest explanation of the evidence is that the common ancestor of today's bony fish had lungs and that lungs turned into swim bladders in the lineage that led to teleosts. The oldest fossils of bony fish all come from marine waters, where fish presumably didn't have to worry too much about running low on oxygen. Colleen Farmer, a physiologist at the University of Utah, thinks that the evolution of lungs made fish better swimmers. In air-breathing fish, some of the blood that flows through the gills gets diverted to the heart, and when these fish swim hard, they tend to breathe more air.
Farmer suggests that they're trying to keep their hearts supplied with enough oxygen.
And that, she proposes, may have been the initial pressure driving the evolution of the first lungs some 400 million years ago. Farmer speculates that the change started when rising to the surface to breathe became risky. Perhaps fish species that lost their lungs flourished, while those with lungs became rare. Fortunately for humans, one lineage of lunged fish hauled ashore about 360 million years ago, eventually giving rise to land-dwelling amphibians, reptiles, and mammals. In the millions of years that followed, these vertebrates have retooled their hearts in multiple ways.