给出一个数组,在该数组上进行O(n)比较以找到要插入的索引,然后进行插入,时间复杂度为O(n ^ 3)吗?
由于每个元素(n),您都将遍历已排序的列表(n),然后插入(n)。
据我了解,正常的实现没有任何插入,只有交换,这可以将其减少为O(n ^ 2),因为通过交换而不是插入将项目放置在正确的位置。
用于O(n ^ 3)插入排序的伪代码:
for element in array
find the correct location
then insert in the correct location