sorting algo with min writes