The shape of a collapsing cloud is influenced by various factors, including the initial shape, angular momentum, ability to dissipate heat, and sub-clumping processes. When a gas cloud collapses, it can only contract by a factor of 2 in each direction before the rising heat content requires energy dissipation for it to continue its collapse. Generally, gas can dissipate and so the collapse proceeds but quite slowly. If the cloud forms sub-clumps at only a few collapse factors, that is, it rapidly forms stars, globular clusters, or molecular clouds, it maintains its shape at this point. Those gas clouds that were not rotating rapidly before the collapse would be primarily supported through an anisotropic velocity dispersion (pressure from random velocities), not rotational support, and will likely end up in an elliptical shape.
From the distribution of specific angular momentum of primordial gas clumps in cosmology simulations, one expects around 20% of first collapse galaxies would be ellipticals. This is about the fraction of galaxies that are elliptical and indicates that ellipticals formed by mergers are a minority of all elliptical.
For a gas clump that possesses significant angular momentum and sub-clumps slowly enough, it will collapse along the rotation axis into a flat rotating disk. This is the Population II stars in spirals. The stars that formed earlier, before the clump formed a flattened disk, compose the elliptical shaped stellar halo of Population I stars.
The halo plays an essential role in this process. With a dissipating gas cloud embedded in a non-dissipating halo, the dynamics become more complex. Because the universe began as a homogeneous expanding gas, there is very limited angular momentum. All angular momentum is picked up by tidal interactions between neighboring density enhancements. The dark matter halo only collapses by a factor of two, enabling the baryonic matter (ordinary matter) to collapse by a few extra factors which leads to higher rotational velocities that we observe in galaxies. Without the influence of this dark matter, more galaxies would be ellipticals.
The prevalence of ellipticals in clusters does not support the picture that all ellipticals are formed by mergers. Mergers between galaxies require velocities to be closely matched. This is not the case in clusters because they have high velocity dispersions. Thus, the merger rate is low in clusters once it forms. This suggests that the elliptical galaxies in clusters are primordial rather than formed by the late merger process.
There are a couple of potential explanations for the preferential formation of ellipticals in clusters. Firstly, galaxies forming in clusters may have collapsed into stars at a faster rate due to the higher densities present. The dense environment could lead to more efficient star formation processes, resulting, as described above, in the formation of elliptical galaxies.
Secondly, the process of tidal torque spin-up, which contributes to the formation of spirals, might be less effective in cluster environments. Tidal torque spin-up typically requires pure expansion to avoid tidal locking.