sort input array before any operation