归并排序(merge sort)是一个时间复杂度为O(nlogn)的基于比较的排序算法(comparison based sorting algorithm)。 归并排序大多数实现(implementation)都将其实现成了一个stable sort, 所谓的stable sort的意思就是the implementation preserves the input order of equal...
分类:
编程语言 时间:
2014-07-28 15:55:43
阅读次数:
392
之间介绍插入排序时漏掉一种插入方式,那就是折半插入。
这种方式是采用二分查找法去查找插入点,可以减少元素比较次数,但是并不能减少移动次数,复杂度跟直接插入一样,都为O(n^2).
直接上代码:
//二分插入排序
void binary_insert_sort(int arr[],int len)
{
if(arr == NULL || len <= 1)
{
return;
}...
分类:
其他好文 时间:
2014-07-28 00:27:19
阅读次数:
292
二分查找算法是一种在有序数组中查找某一特定元素的搜索算法。搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组 为空,则代表找不到。这种搜索算法每一次比较都使搜索范围缩小一半。折半搜索每次把搜索区域减少一半,时间复杂度为Ο(logn)。...
分类:
其他好文 时间:
2014-07-27 23:52:09
阅读次数:
270
素数打表,加上sum[i]数组表示1-i中有多少个素数二分查找#include #include using namespace std;#define max 1000005int phi[max],sum[max];int a,b,k;void getp(){ for(int i=2;i>a>>...
分类:
其他好文 时间:
2014-07-27 23:27:19
阅读次数:
355
关于二分查找,这绝对是最简单却又最难的实现了,其各种版本号能够參见http://blog.csdn.net/xuqingict/article/details/17335833在C++的标准库中,便提供了这种函数,lower_bound 与 upper_bound,对于这两个函数的理解,有例如以下几...
分类:
编程语言 时间:
2014-07-27 21:30:35
阅读次数:
306
蛮常见一道题目。
思路:
1:排序,按顺序遍历两个数之和twoSum,
2: 二分查找 (0 - twoSum)看是否存在
这题最容易错的地方是must not contain duplicate triplets,所以遍历的这时候要用一个数字记录最后一个遍历的数字是,避免重复。
#include
#include
#include
using namespace s...
分类:
其他好文 时间:
2014-07-27 11:26:22
阅读次数:
260
用了和3Sum差不多一样的思路,二分查找。关键要剪枝,但是却在剪枝那里犯了很多错误。
然后原来有一个更加快的思路O(n^2).
#include
#include
#include
#include
using namespace std;
class Solution {
public:
int threeSumClosest(vector &num, int ta...
分类:
其他好文 时间:
2014-07-27 11:21:04
阅读次数:
165
Implement int sqrt(int x).Compute and return the square root of x.线性查找会TLE。用二分查找。注意溢出的处理。全部都改成long long. 1 class Solution { 2 public: 3 int sqrt(i...
分类:
其他好文 时间:
2014-07-27 10:41:02
阅读次数:
181
#includevoid MergeArray(int first,int mid,int last,int a[]){ int k=0; int i=first,j=mid+1; int n=mid,m=last; int c[100]; while(i<=n && ...
分类:
其他好文 时间:
2014-07-26 00:40:06
阅读次数:
216