今天在学校,老师要求我们实施重复删除算法.这并不困难,每个人都想出了以下解决方案(伪代码):
for i from 1 to n - 1
for j from i + 1 to n
if v[i] == v[j] then remove(v, v[j]) // remove(from, what)
next j
next i
Run Code Online (Sandbox Code Playgroud)
这个算法的计算复杂性是n(n-1)/2.(我们在高中,我们没有谈过大O,但似乎是O(n^2)).这个解决方案看起来很难看,当然也很慢,所以我试着更快地编写代码:
procedure binarySearch(vector, element, *position)
// this procedure searches for element in vector, returning
// true if found, false otherwise. *position will contain the
// element's place (where it is or where it should be)
end procedure
----
// same type as v
vS = …Run Code Online (Sandbox Code Playgroud)