The vast majority of ships will sink if they are overturned by the waves, or if an opening is formed in the hull of the ship through which water suddenly enters.
Some sailboats have ballast that returns them to an upright position if the wind overturns them, but they will also sink if larger holes are formed in the hull.
The problem of sinking could be prevented very easily, by installing elements that have a large enough volume with which they can hold the entire weight of the ship on the surface of the water together with the crew and cargo. These elements would act as a raft that keeps the ship afloat.
Each boat, sailboat, or larger boat on the side has a railing that prevents the crew from falling over the edge of the boat into the water while rocking the boat. For large boats and fishing boats, this fence is made of steel plates. For sailboats and smaller boats, this fence is made of small poles connected by rope or steel cables.
The fence could also serve as protection against sinking the ship, as shown in the picture above.
In place of the railings on the boat (1) could be placed special lightweight elements (2) that have two functions.
The first function would be to serve as a fence against falling people and cargo from the ship (1).
Another function would be to keep the entire weight on the surface of the water in case of overturning of the ship (1) or entry of water into the ship (1). In order for this to be possible the light elements (2) would have to have a very small mass, and their total volume would have to be slightly greater than the total mass of the ship (1) with crew and cargo . These lightweight elements (2) could be made of thin sheet metal, rubber or plastic, and filled on the inside with solid epoxy foam, which is mostly used in construction. These lightweight elements (2) would be fastened to the boat or sailboat with screws, welding or gluing. In large ships, they would be easiest to weld, while in smaller ships they could be glued, and if they sometimes need to be removed, they could be connected to the ship with screws.
In this way, ships could become unsinkable with a small addition to the price. In the event of a rollover, they would return to an upright position on their own, and in the event of a water intrusion, they could be towed by other ships to the nearest overhaul port.