We present two algorithms that are near optimal with respect to the number of inversions present in the input. One of the algorithms is a variation of insertion sort, and the other is a variation of merge sort. The number of comparisons performed by our algorithms, on an input sequence of length n that has I inversions, is at most $$n\,{\rm log}_{2} (\frac{I}{n} + 1) + O(n)$$ . Moreover, both algorithms have implementations that run in time $$O(n\,{\rm log}_{2} (\frac{I}{n} + 1)\,+\,n)$$ . All previously published algorithms require at least $$cn\,{\rm log}_{2} (\frac{I}{n} + 1)$$ comparisons for some c > 1.