Introduction to Algorithms 2.3-7

Source: Internet
Author: User

problem: Given the set S of N integers and another integer x, describes an algorithm that runs at O (log N), which determines if there are two elements in s that are exactly X

Algorithm Description:

1. Sort the elements of the set in array a first

2, for each element in the set A[i], in a sorted array a binary find X-a[i]

3, the search success exists, the end of the loop after the search is not successful does not exist

Pseudo code:

1 Sort (A) 2 n = a.length3for1 to n4     1, N) /c12>5     if ans! = NotFound6         returntrue 7 return false

Algorithm Complexity Analysis:

T (n) = log 1 + log 2 + log3 +....+logn = log (n!) = log (sqrt (2*pi*n) * (n/e) ^n) = O (n log n)

Complexity of O (LOGN)

Introduction to Algorithms 2.3-7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.