Introduction to Algorithms Learning Notes (3)-Exercise 2.3-7-sorting + two points

Source: Internet
Author: User

Question (test instructions):

Describe a O (n LG (n))-time algorithm, given a set S of n integers and another integer x, determines whether or not th ere exist elements in S whose sum is exactly x.

Design an O (n LG (n)) time complexity algorithm, given a set of n numbers, and a number x, asking if it is possible to find two numbers in this set, their and exactly X.

Solution (Solution):

In an array to find a certain number, O (n LG (n)), it is easy to think of sorting + two points, the introduction of the algorithm has just talked about the merge sort, is just the O (n LG (n)) sorting algorithm, but this question is to find two number of and X, then how to do it, we can in the sorted array From small to large enumerate the first number (the number must satisfy <=X/2, reason to think), and then find out if there is another number, the following is the code:

Code (CODES):

Introduction to Algorithmic exercises 2.3-7#include <iostream>using namespace std;void merge (int* A, int p, int q, int r) {int N1 = Q-p +1,    N2 = R-q;        int l[n1], r[n2];    for (int i = 0; i < N1; i++) l[i] = A[p+i];    for (int i = 0; i < n2; i++) r[i] = a[q+i+1];    int i = 0, j = 0, k = p;            while (K <= R) {if (I >= N1) {while (J < N2) {a[k++] = r[j++];            }} else if (J >= N2) {while (I < N1) {a[k++] = l[i++];        }} else if (L[i] <= r[j]) {a[k++] = l[i++];        } else {a[k++] = r[j++];        }}}void Merge_sort (int* A, int p, int r) {if (P < r) {int q = (p + r)/2;        Merge_sort (A, p, q);        Merge_sort (A, q+1, R);    Merge (A, p, Q, R);        }}bool bsearch (int* A, int L, int R, int v) {while (L <= r) {int M = (L + r)/2;        if (A[m] < v) {L = m+1;        }else if (A[m] > V) {R = M-1;        } else {return true; }} return false;}        BOOL Find (int* A, int n, int x) {merge_sort (A, 0, n-1);                for (int i = 0; i < n; i++) {if (A[i] <= x/2) {if (bsearch (A, i+1, N-1, X-a[i])) {            return true;        }} else {return false; }} return false;}        int main () {int a[10] = {2, 3, 1, 5, 3, 2, 9, 3, 0, 7}, X; while (CIN >> x) {if (Find (A, ten, X)) {cout << "find" numbers whose sum is "        << x << Endl;        } else {cout << ' not ' find numbers whose sum is ' << x << Endl; }} return 0;}



Introduction to Algorithms Learning Notes (3)-Exercise 2.3-7-sorting + two points

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.