How to sort nearly sorted array in the fastest time possible? (Java)

前端 未结 10 2128
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 06:14

I have an array of values which is almost, but not quite sorted, with a few values displaced (say, 50 in 100000). How to sort it most efficiently? (performance is absolutely

10条回答
  •  一向
    一向 (楼主)
    2020-12-30 06:58

    Just to put it on the table, a well implemented bubble-sort would certainly be the simplest algorithm here. With a worst-case of O(n*m), m being the number of displacements. The m part depends heavily on the pattern of displacements, usually total complexity would be O(n).

提交回复
热议问题