Fastest way to search for an element in unsorted array

后端 未结 10 1785
北恋
北恋 2020-12-14 08:13

I just bumped on to this question today and was trying for a solution that is better than O(N) but could not come up with one.

Searched through SO but couldn\'t find

10条回答
  •  一整个雨季
    2020-12-14 09:03

    What will be the efficiency of algorithm that makes use of partition approach applied during quick-sort as follows?

    1. Randomly select some value (let us call it v) in the list.

    2. Partition the entire list into 2 parts. Left part contains all elements that are less than v. Right part contains all elements that are greater than v.

    3. Repeat the steps 2, 3 until you determine whether the element exists or does not exist.

    I am not sure about the complexity of above algorithm, but looks like it will be definitely less than the complexity of quick-sort algorithm: (n log n).

提交回复
热议问题