摘要:
请给出一个运行时间为O(n lgn)的算法,使之在给定一个由n个整数构成的集合S和另一个整数x时,判断出S中是否存在有两个其和等于x的元素。以下思路一 和 思路二来自http://blog.csdn.net/magic_coder/article/details/6435098思路一 :我们最容易想到的是O(n2)的算法,大致伪码即:1 findX(A, x){2 for i=0 to length[A] {3 key = A[i]4 for j=0 to length[A]{5 if(j != i && key + A[j] == x ){return true}6 }7 r 阅读全文
摘要:
/* * binarySearch.cpp * 算法导论 P.23 2.3-5 * Created on: 2011-12-27 * Author: LiChanghai */#include <iostream>#include <cstdlib> using namespace std; template <typename T>size_t binarySearch(const T array[ ], size_t pBegin, size_t pEnd, T key) { //当只有2个元素的时候不再二分,直接比较 if(pBegin+1 == pE 阅读全文