随笔分类 - [41]Algorithm算法
-
基于内存共享的并行排序算法慢谈
摘要:基于内存共享的并行排序算法慢谈(上)1.前言并行程序设计的重要性我就不多说了,可悲的是明知它重要,我却没学好。这不还是碰到了,请听题:请用Python多线程对一个4G以上的文件,进行外排序,尽量优化性能。假设系统内核数为8,Mem=512MB,关键字是字符串记得上学期开始上并行程序设计的时候,我还是激情澎湃的。但是后来就萎了。每次上手一样新东西,我都会到园里翻翻看,但是不幸的是园里关于这块好的文章不太多。我不知道是什么原因,可能是现成的东西太多了吧。这块骨头不好啃啊,所以打算分几个篇幅来说。本文的主题是-基于内存共享的并行排序算法。本人水平有限,如果有什么不恰当的,请大家一定不要吝啬指正,这也 阅读全文
-
2012百度之星资格赛试题与AC代码合集
摘要:百度文库下载地址:http://wenku.baidu.com/view/39d92648852458fb770b5616.html2012百度之星资格赛试题与AC代码A:百度计算器的加法时间限制: 1000ms 内存限制: 10000kB描述百度框计算中提供了计算器这个功能,模拟计算器中的复杂功能,我们最先需要解决的就是实现加法模块。今天就给你个机会,和百度计算器一样,计算一下十以内的加法吧。输入仅有一组数据,包含两个正整数,分别为a, b(0 <= a, b <= 10)输出一个正整数,暨输入a, b后对应的a+b的计算结果样例输入5 2样例输出7 1 #include < 阅读全文
-
串的模式匹配算法
摘要:这一节介绍一下由Rabin和Karp提出的RK算法。1,RK算法的基本思想 HASH! 如果两个字符串hash后的值不相同,则它们肯定不相同;如果它们hash后的值相同,它们不一定相同。 RK算法的基本思想就是:将模式串P的hash值跟主串S中的每一个长度为|P|的子串的hash值比较。如果不同,则它们肯定不相等;如果相同,则再诸位比较之。2,RK算法的求解过程 将我们用来比较的字符串的全集设为∑={a,b,…,z},设∑的长度为d=|∑|,则主串和模式串都可以看作是d进制数。例如只由数字组成的字符串,它的全集∑={0,1,2,3,4,5,6,7,8,9},d=10。 设模式串为P,其长度为m 阅读全文
-
二叉查找树5
摘要:接上一篇,继续讲二叉查找树的操作,之前的博客都讲得差不多了,本篇就讲一下删除操作,以及求最矮公共父结点(LCA:lowest common ancestor)的操作吧。删除 将一个结点从二叉查找树中删除之后,剩下的结点可能会不满足二叉查找树的性质,因此,在删除结点之后要对树进行调整,使其满足二叉查找树的性质。根据结点的孩子的数量,将删除操作分为三种情况,我们记要删除的结点为z,实际上删除的结点为y。 1. z结点没有孩子。 如下图a所示,我们要删除值为13的结点,因为结点没有孩子,所以删除之后不会影响到二叉树的整体性质,也就是说,直接将13这个结点删除即可,如图a所示,从左边的二叉树删除... 阅读全文
-
二叉查找树
摘要:接上一篇,让我们来继续讨论二叉查找树的基本操作,需要注意的一点是,上篇中的遍历操作是针对任意一棵二叉树都可以的,凡是没提及需要二叉查找树性质的地方,应该都是满足二叉树的操作。上篇主要讨论了二叉树的遍历操作,这篇,我们来讨论二叉查找树的查找、求最大最小值以及求前驱和后继等操作。查找 二叉查找树的查找操作可以在O(h)时间内完成,其中h为树的高度。查找的算法很简单,根据二叉查找树的性质,我们先将要比较的元素跟根元素相比较,如果相等则返回,如果大于根结点的key,则继续在右子树中查找,如果小于根结点的key值,则在左子树中查找。这也跟插入过程类似,童鞋们可以想象一下,我就不画图了,画图很麻烦。 .. 阅读全文
-
递归优化之快速排序
摘要:很多人都说.net排序的效率高,抱着学习的态度观摩.net源代码。当数据类型为基本类型并且未指定排序规则时用的是本地代码(TrySZSort),否则使用快速排序的泛型实现。QuickSort经测试TrySZSort的运行时间为快速排序泛型实现的1/2左右,据推测TrySZSort与QuickSort采用的是同样的算法,不过TrySZSort的实现是本地代码的指针模式。快速排序是我所知道的通用排序方法当中实际平均运行效率最高的,平均时间复杂度为O(n*log(n)),当然最坏情况可能是O(n^2),但是这种概率也是指数级的。很多人实现的快速排序都有一个致命缺陷,那就是没有控制递归深度,等于给自己 阅读全文
-
二叉查找树(三)
摘要:我们知道二叉查找树是一种数据结构,它支持多种动态集合的操作,包括:查询,最大值,最小值,前驱,后继,插入和删除等操作。那么我们在前一篇已经创建了二叉查找树,那么我们来实现二叉查找树的各种操作吧。(*^__^*) (以下纯属个人理解,个人原创,理解不当的地方,请指正,谢谢) 我们来看二叉树的遍历操作,所谓遍历,顾名思义,就是能够依次的访问二叉查找树中的各个结点。在数据结构课堂上,我们知道,遍历方式有深度优先和广度优先遍历,深度优先又包括前序、中序和后序遍历;广度优先遍历,在二叉树的范畴中,就是层序遍历,就是先遍历某一层的结点,然后再遍历下一层的结点。好了,一个个的来介绍吧。前序遍历 前序遍历.. 阅读全文
-
算法题
摘要:好久没有做算法题了,重温几个简单的算法题。第一题:求子数组的最大和这是一道很常见的算法题,很多人都能很快的写出算法,但很多人都不能写得完全正确,问题主要出在sum初始化上,很多错误的答案将他初始化为0,如果数组的所有元素都为负,那么得到的最大最是0,sum要初始化成数组的第一个元素。第二题:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句这道题在网上也有很多个版本,有在构造函数中实现加法,利用两个静态变量一个存结果,一个存当前值,然后创建一个一维n个元素的数组,存结果的静态变量即为所求,还有的就是用两个方法,一个方法是递归 阅读全文
-
8大排序3大查找
摘要:每天都在叫嚣自己会什么技术,什么框架,可否意识到你每天都在被这些新名词、新技术所迷惑,.NET、XML等等技术固然诱人,可是如果自己的基础不扎实,就像是在云里雾里行走一样,只能看到眼前,不能看到更远的地方。这些新鲜的技术掩盖了许多底层的原理,要想真正的学习技术还是走下云端,扎扎实实的把基础知识学好,有了这些基础,要掌握那些新技术也就很容易了。要编写出优秀的代码同样要扎实的基础,如果排序和查找算法学的不好,怎么对程序的性能进行优化?废话不多说,本文要介绍的这些排序算法就是基础中的基础,程序员必知!1、直接插入排序(1)基本思想:在要排序的一组数中,假设前面(n-1) [n>=2] 个数已经 阅读全文
-
数值压缩存储方法Varint
摘要:smark提供可靠、高性能的Socket Tcp组件数值压缩存储方法Varint 在编写网络通讯的时候我们经常需要把一些数据存储到byte[]中然后再发送出去,数值则是我们经常处理的数据成员。发越少的东西意味着使用更少的IO和带宽 ,所以对传输数据进行压缩也是件非常重要的事情。接下来提到的就是一种基于数字存储的方式在大多数情况下可以节省数值存储空间。 Varint 是一种紧凑的表示数字的方法。它用一个或多个字节来表示一个数字,值越小的数字使用越少的字节数。这能减少用来表示数字的字节数。比如对于 int32 类型的数字,一般需要 4 个 byte 来表示。但是采用 Varint,对于很小的 .. 阅读全文
-
算法研究学习一
摘要:(1)插入排序插入排序是逐个处理待排序的记录,每个新记录与前面已排序的子序列进行比较,将它插入到子序列中正确的位置 ,如下图所示。c语言实现代码:View Codec#语言实现代码View Code(2)冒泡排序C语言实现代码:View CodeC#实现代码:View Code(3)选择排序选择排序第i次是选择数组中第i小的元素,并将该记录放到数组的第i个位置。换句话说,首先从未排序的序列中找到最小关键码,接着是次小的,依次类推,独特之处是很少交换。选择排序和冒泡排序比较相似,区别在于冒泡排序找到的最小元素是通过不断的交换相邻位置的值得到的,而选择排序只是先找到最新元素的位置,相当于数组的下标 阅读全文
-
有意思的排序算法-快速排序
摘要:快速排序对于含有n个元素的数组,最坏情况的运行时间为O(n2),虽然这个最坏情况的运行时间比较差,但是快速排序通常是用于排序的最佳的实用选择,这是因为其平均性能相当好,而且我们可以采用随机化的快速排序算法,来减少出现最坏情况的机会,其期望运行时间为O(nlgn),而且该记号中含的常数因子很小。 像合并排序算法一样,快速排序也是基于分治法进行排序的。其排序过程分为三个步骤: 分解:数组A[p..r]被划分为两个(可能空)子数组A[p..q-1]和A[q+1..r],使得A[p..q-1]中的每个元素都小于等于A(q),而且小于等于A[q+1..r]中的元素。下标q也在这个划分过程中计算。 ... 阅读全文
-
有意思的排序算法-堆排序
摘要:堆排序,是一个非常优秀的排序算法,像合并排序而不像插入排序,其运行时间为O(nlgn),像插入排序而不像合并排序,它是一种原地排序算法,所以说,堆排序将插入排序和合并排序的优点结合起来了。 堆排序借助于堆数据结构,(二叉)堆是一个数组,它可以被视为一棵完全二叉树,树中每个节点与数组中存放该节点值得那个元素对应。 堆排序算法可以分为以下几步: 1) 建立原先数列对应的最大(或最小)堆。 2) 重复n-1次循环,每次选出一个最大(或最小)值,并放到合适的位置。 Java代码实现如下: 1 public class HeapSort implements SortAlgorithm { ... 阅读全文
-
有意思的排序算法-插入排序
摘要:所谓排序,无非就是把一个无序的序列排成一个有序的序列,从本文开始,将着重介绍经典的一些排序算法。 插入排序,是指将待排序列中的数,一个一个插入到适当位置的过程。说起算法的概念来,总是让人摸不着头脑,还是从生活中的例子来理解吧。相信每个人都玩过牌,我们在开始摸牌的时候,左手是空的,牌面朝下放到桌子上,接着,一次从桌子上摸起一张牌,并将它插入到左手一把牌中的正确位置上,为了找到这张牌的正确位置,要将它与手中已有的每一张牌从右到左地进行比较,无论什么时候,左手中的牌都是排好序的,而这些牌原先都是桌子上那副牌里最顶上的一些牌。 于是,根据以上的算法分析,可以写出以下的Java代码。 1 /** 2 . 阅读全文
-
有意思的排序算法-合并排序
摘要:合并排序也可以用打牌的过程来说明,假设桌面上朝上放着两摞已经排好序的牌,现在要将这两摞已排好序的牌合成一摞,首先,取两摞中位于最上面的两张中最小的一张并将其加入到新的一摞中,然后接着从两摞中再取一张最小的加入到新的一摞中,因为第二张,肯定比第一张要大,因此要加入到第一张的后面才行。 从上面可以看出,合并排序是利用分治法进行排序的算法,直观地操作如下: 分解:将n个元素分成各含n/2个元素的子序列; 解决:用合并排序法对两个子序列进行递归地排序; 合并:合并两个已排序的子序列以得到排序结果。 我就不写伪代码了,直接用Java将其实现的代码如下: 1 /** 2 * 合并两个排... 阅读全文
-
数据结构之排序算法--C#实现
摘要:快找工作了,要整整算法之类的东西了,先把内功练好了才会有更好的发展,从今天起开始写些数据结构的东西。东西可能会比较简单,作为入门,大家可以也学习学习。并提出些修改意见。排序算法之一:冒泡排序(Bubble Sort)冒泡排序算法是可用的最慢的排序算法之一,但是是最容易理解和实现的一种排序算法。这种排序的得名是由于数值"像气泡“一样升至队列的顶端或者底端而得名,通过多次遍历整个列,并且比较相邻的数据,如果左边的数值大于右边的数值就进行交换(升序)。实现代码如下:Bubble Sort Code 1 public static void BubbleSort(int[] arr) 2 . 阅读全文
-
海量数据 处理方法总结
摘要:大数据量,海量数据 处理方法总结(转)1.Bloom filter适用范围:可以用来实现数据字典,进行数据的判重,或者集合求交集基本原理及要点:对 于原理来说很简单,位数组+k个独立hash函数。将hash函数对应的值的位数组置1,查找时如果发现所有hash函数对应位都是1说明存在,很明显这 个过程并不保证查找的结果是100%正确的。同时也不支持删除一个已经插入的关键字,因为该关键字对应的位会牵动到其他的关键字。所以一个简单的改进就是 counting Bloom filter,用一个counter数组代替位数组,就可以支持删除了。还 有一个比较重要的问题,如何根据输入元素个数n,确定位数组m 阅读全文
-
一道面试附加题的另类求解
摘要:有一段时间没有写博客了。今日闲逛的时候,看到一篇博客“4月7日某公司在华南地区举办了一年一度的"开发者"聚会——记某公司笔试”。里面有作者回忆的面试题。其中一题引起了笔者的注意,题目如下: 题目:已知一个数组a[N],构造一个数组b[N],构造规则:b[i]=a[0]*a[1]*a[2]...a[N]/a[i]; 要求: 1、不可以使用除法; 2、时间复杂度为O(n),空间复杂度为S(0); 3、除遍历使用的变量外,不可以使用其它变量; 看似简单,想想也废了一番脑筋。 最先想到的是就是原作者想到的方法,代码如下(用的是VB2008): PublicShared... 阅读全文
-
模拟LRU算法&通道处理算法
摘要:最近写了两个程序,模拟操作系统的算法,只是基本实现了课本上的基本功能,实际应该是很复杂的。模拟LRU页面置换算法: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #define TN 3 //分配给该程序的主存页数 6 #define PN 10 //地址流 7 8 int page[TN],cnt[PN]; //主存页面表 9 typedef struct PAGE{10 int kPage; //the kth line 分配给该程序的主存页数的索引11 int 阅读全文
-
字符串的逆序之旅
摘要:这两天在看《编程珠玑》,第一章就收获非常的多,真的挺后悔现在才看着本书,第二章有个将字符串逆序的例子,就是比如“thisisastring”变成“stringaisthis”,那么今天就总结一下这个逆序字符串的小专题。首先假设有人问你:如何将“thisisastring”完全逆序,也就是gnirtsasisiht方案一:申请一个同样大小的空间,直接逆序将字符串保存一遍。 这是我们最容易想到的一种方法,我们只需要找到字符串尾指针就好了,下面就是一段简单的代码:这个代码的关键是找准字符串尾的位置,(每一个字符串以‘\0’结... 阅读全文