Algorithm - the time complexity of deletion in a unsorted array

后端 未结 4 1116
庸人自扰
庸人自扰 2021-01-01 03:29

Suppose there is a unsorted array A, and it contains an element x (x is the pointer of the element), and every element has a satellite variable k. So, we can get the followi

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 04:20

    Finding the element with a given value is linear.

    Since the array isn't sorted anyway, you can do the deletion itself in constant time. First swap the element you want to delete to the end of the array, then reduce the array size by one element.

提交回复
热议问题