Hanging of all pirates as they're found ensures each will not pose a threat in future. It was once the law of the seas to do just that. Instead, we jerk around with the "legalities" of it all.
We'll never see oceans devoid of piracy, so long as there is such high value available for the taking. Heavily-armed ships would make a lot of sense, were it not for the extreme costs involved. Today, basically insurance covers much of the lost value. It seems to me that it's considered simpler and more expedient than outfitting vessels with the means of repelling invaders. Seriously, even in the Gulf of Aden, it's an incredibly huge expanse of water with a large number of high-value ships in the lanes. Doing just what you're suggesting has proven difficult (via the use of military ships and "SEALs").