Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 2.3-7 #456

Open
IgorSuharev opened this issue Jul 13, 2022 · 1 comment
Open

Exercise 2.3-7 #456

IgorSuharev opened this issue Jul 13, 2022 · 1 comment

Comments

@IgorSuharev
Copy link

IgorSuharev commented Jul 13, 2022

After sorting of S, a search of two a, b in S such that a + b = x can be improved.

FIND-SUM(S, x) // Indexes in S are 1, 2, ..., n
S = SORT(S)
i = 1
j = n
while i < j
    sum = S[i] + S[j]
    if sum == x
        return TRUE
    if sum < x
        i = i + 1
    else
        j = j - 1
return FALSE

Using this way, the time complexity of the search is Θ(n) instead of Θ(n lgn). (But, it's obviously that time complexity of entire algorithm stays same.)

@funny1dog
Copy link

Yes, you are right, but the final time complexity stays the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants