随笔分类 - [41]Algorithm算法
-
二叉树基本操作
摘要:数据结构复习-二叉树基本操作引言 近日受人所托,搞了点二叉树的程序,顺便回顾了下二叉树的一些基本知识,特此总结。 二叉树的基本操作,可能包括: 创建,遍历,转化,复制,删除等。 遍历:前中后三种顺序的遍历,已经是各数据结构与算法教程的最基础内容,在此不重复。创建:大多数据结构教程当中的二叉树创建程序,都是采用的递归方式,递归方式创建的二叉树与遍历的过程相似,所创建的二叉树,也是采用左右子节点方式,后续进行遍历操作十分方便。转化:直觉上,最简单的二叉树存储方式其实是如下图的数组:*此图出自某高校数据结构ppt,但实在难以查证是哪个学校,无法直接感谢,请谅解。首先,提供个满二叉树大小的数组,然后其 阅读全文
-
百度面试题:求绝对值最小的数
摘要:百度面试题:求绝对值最小的数有一个已经排序的数组(升序),数组中可能有正数、负数或0,求数组中元素的绝对值最小的数,要求,不能用顺序比较的方法(复杂度需要小于O(n)),可以使用任何语言实现例如,数组{-20,-13,-4, 6, 77,200} ,绝对值最小的是-4。算法实现的基本思路找到负数和正数的分界点,如果正好是0就是它了,如果是正数,再和左面相邻的负数绝对值比较,如果是负数,取取绝对值与右面正数比较。还要考虑数组只有正数或负数的情况。我根据这个思路用Java简单实现了一个算法。大家有更好的实现方法欢迎跟帖?123456789101112131415161718192021222324 阅读全文
-
素数算法
摘要:浅析求素数算法Posted on2010-03-03 22:01linFen阅读(4802) 评论(1)编辑收藏算法2009-12-05 16:15:31阅读18评论0字号:大中小浅析求素数算法时间: 2006-10-27注意: 如果没有特殊说明, 以下讨论的都是针对n为素数时的时间复杂度1. 根据概念判断:如果一个正整数只有两个因子, 1和p,则称p为素数.代码:bool isPrime(int n){ if(n < 2) return false; for(int i = 2; i < n; ++i) if(n%i == 0) return false; r... 阅读全文
-
搜索引擎的评价
摘要:这篇就简单介绍一下搜索引擎的评价方法。从用户的角度去评价一个搜索引擎的检索效果最好的方法就是计算用户在查到自己满意文档时已经浏览的文档数。但是实际中,查询千变万化,文档也千变万化,所以这种方法不可行。人们便提出了下面的概念,并建立了一个评价标准。这里面有三个常用的概念:正确率,精确率,召回率。正确率(Precision,简称为P)定义为:P=返回结果中相关文档个数/返回结果的数目。精确率(accuracy,简称为A)定义为:A=判断结果正确的文档数目/所有文档数目。召回率(Recall,简称为R)定义为: R=返回结果中相关文档数目/所有相关文档数目。实际相关文档数实际不相关文档数返回文档数( 阅读全文
-
浅谈排序
摘要:排序的现实意义每个人都知道,今日突然对各种排序算法产生了兴趣,于是谈下我所接触到的一些排序算法:选择,插入,冒泡,快排,堆排,归并,基数,计数,bogo。代码除个别都由本人所写,若有 bug 请您指正。个人建议,一定要看看计数排序,别的快排和归并排想法也很好。选择和插入很基本,也是最容易理解的,最逆天的就是 bogo 了,一定要看哦!!注:将input函数和output函数还有主函数写在了冒泡里,以后就直接写排序函数的代码了,其他部分的代码的输入输出函数、主函数和冒泡排序基本一致。1.冒泡排序 描述:依次比较相邻的两个数,将小数放在前面,大数放在后面。这样每次都能将最大的数放在最后,多次迭代. 阅读全文
-
逻辑
摘要:一篇文章点醒我现在有A,B,C,D,E五个人,要分配他们当中若干人等去做任务,但是这几个人性格乖张,必须要满足以下几个条件: 若A去,B也要去D,E两个人中至少要一人去,B,C两人中只能去一人,C,D两人要么都去,要么都不去,E若是去,则A和D都要去。 现在我应该排谁去才能满足以上几个人的要求呢? 最好用文氐图或者卡诺图表示出来。不要用语言这种感觉上正确的工具来表示。要是不会的话,请管住你的手,不要乱回复,谢谢……当遇到上面这类问题的时候,我们怎么办?用脑子想么?我的脑子是经不起这么折腾的……我还是相信形式逻辑的……不过形式逻辑有一些怪论让我百思不得其解,困扰了我好久,因为好多结论跟生活完全背 阅读全文
-
算法导论-1.排序算法
摘要:排序算法是最基础的一类算法。主要排序算法包括选择排序、插入排序、冒泡排序、合并排序、堆排序和快速排序。把这些排序算法全部实现一边,再把《算法导论》对应章节后面的习题做一遍,确实是系统学习算法的一个不错的开端。选择排序选择排序的想法很简单,把需要排序的数组看成一堆扑克牌:先查一遍,抽出最小的作为第一个张;在剩下的牌堆里再查一遍,选出最小的作为第二个元素……重复直到牌堆耗尽。想法简单的其代价就是运行时间为Θ(n×n):在查询A、2或3的时候,都要反复地比较其与K、Q的大小,直觉告诉我这样做没有意义。void selectionSort(int* x, int length){ for(i 阅读全文
-
算法
摘要:一道百度之星编程大赛题的随笔联想·(1)百度之星,是全球最大的中文搜索引擎,百度公司面向中国高校学生和编程爱好者所举办的高水平的程序设计大赛。他所考试的题目,全部都是算法的题目。鄙人虽然是一个.net程序员,在工作之余,喜爱算法。 我觉得这个题目有点意思,故而分享给大家,我想到两种方法,提供大家,希望对大家起了一个开阔思路的作用。首先,看题目是那样的:请编写程序,根据输入的任何一个正整数,找出符合这种要求的所有连续正整数序列。输入数据:一个正整数,以命令行参数的形式提供给程序。输出数据:在标准输出上打印出符合题目描述的全部正整数序列,每行一个序列,每个序列都从该序列的最小正整数开始、 阅读全文
-
数据结构与算法学习--链表
摘要:链表分为单链,双链和循环链表,链表C语言实现://单链typedef struct NodeType{ char elem; NodeType * next;} Node;//双链typedef struct DuobleNodeType{ char elem; NodeType *next; NodeType *prev; }DoubleNode;1.单链表反转。以微软的一道面试题为例,编写一个函数,给定一个链表的头指针,要求只遍历一次,将单链表中的元素顺序反转过来。给出反转函数:Node* LinkList_reverse(Node* head) { Node *p... 阅读全文
-
算法设计和数据结构学习堆排序
摘要:前言 这时上次学妹课程的一道作业题,我花了点时间做了下,其题目内容为: 试写一程序,可以对一二元树(binary)进行堆积排序(heap sort) (a)使用者可自己决定输入二元树的节点个数 (i)node数不超过50 (b)节点值由随机方式产生,并印出随机设值结果 (i)以时间复杂度O(n)的方式设值 (ii)假设值不可重复 (iii)最大值不可大于node数 (例如node数为9,因此最大值为9) (c)使用者可决定使用MAX-HEAP或者是MIN-HEAP来排序 (d)须将重建堆积得过程印出,以及最后输出排序结果 Sample Output: 后面在网上查了下堆排... 阅读全文
-
排序算法
摘要:简单排序算法总结每个算法都接收一个含有元素的数组和一个包含元素个数的整数,我们假设N是传递到排序函数中的元素个数:一、插入排序1、 直接插入排序:最简单的排序算法之一。插入排序有N-1趟(PASS)排序组成,对于P = 1趟到P=N-1趟,插入排序保证从位置0到位置P-1上的元素已经是有序状态(数据结构与算法分析是如此描述的);通俗讲就是把数组分成有序区和无序区,然后通过嵌套的2个循环不断的将无序区的元素调到有序区的适当位置。C语言代码实现如下:void insertsort(int a[],int n){ int p,j; int temp; for(p = 1;p < n;p++){ 阅读全文
-
轮渡模拟
摘要:一、简单介绍 这是在博客园潜水几个月第一次开始想要写点东西,一是记录自己的学习过程,二是和大家分享同时接受大家的指正,再者可以多交一些朋友,我微博地址在公告栏里,互粉哦。。。。这是最近上数据结构时的练习题,首先是队列的实现,再用队列去模拟解决一个实际问题——轮渡模拟。二、问题分析 2.1 问题描述:轮渡模拟:有一个渡口,每条渡船能一次性装载10辆汽车过河,车辆分为客车和货车两类。上渡轮有如下规定:同类汽车先到先上船,客车先于货车上船,轮渡每10分钟一班。模拟一小时内汽车上渡轮的过程。汽车:包含一个汽车类型属性,一个到达时间属性,到达时间随机产生。轮渡:包含一个装车情况的属性,一个出发... 阅读全文
-
你所能用到的数据结构(一)
摘要:无损编码的霍夫曼编码以及其余的各种编码由于要使用比较复杂的数据结构,所以按照我昨天说的,我决定从数据结构开始写起。数据结构和算法很难完全的分开,好的数据结构能够提升算法的效率,而如果没有算法,单纯的谈数据结构,那么数据结构的应用价值就会大大的降低。那么,就从最基本的开始这一个系列吧。一、总是让人很抽象的算法分析算法分析基本是所有数据结构与算法的第一章要讲的内容,大0表示法什么的总是让人很抽象,对于初学者,其实这一章的意义并不是很大,因为你很遇到在实际开发中一些大数据集的问题,在小规模数据的时候,各个算法之间的差别很难分辨出来。这就好比计算5个数的和,大家所用的时间基本都会差不多,但是要是计算5 阅读全文
-
扩展方法之二分查找
摘要:扩展方法之二分查找版本:0.1最后修改:2012-08-08撰写:李现民近期项目策划案调整,要求程序按音乐时间及位置等条件迅速定位当前游戏角色正在使用的动作,因为查询会非常频繁,因此决定使用二分查找。C#类库中有二分查找,分散于Array、List、ArrayList等类中,但接口不太另人满意。对简单的整数数组还好,可以直接使用,但对于复杂的查询,默认的类库使用起来就会比较复杂,比如:classAction{publicstringname;publicinttime;publicintposition;}classTimeComparer:IComparer<Action>{pu 阅读全文
-
所有排序总结(内排序)
摘要:花时间把所有的排序重新 写了一遍。。。。。(应该是认真写过一遍,学的时候根本就没写过) 写得时候才发现,理解不深刻。基本上 只是懂怎么做,不懂为什么。 把我写得记在这里,以后用得着了回来看看。 暂时就到这里吧,以后有时间,继续研究这些东西。在写出来。三个O(n2)的算法选择排序: 1 void SelectionSort(int *a,int n) 2 { 3 for(int i=0;i<n;i++) 4 { 5 int lowindex = i; 6 for(int j=i+1;j<n;j++) 7 if(a[j]<... 阅读全文
-
深入 聚集索引与非聚集索引(一)
摘要:有很多人写了聚集索引和非聚集索引的文章,但我觉得在很多文章中表达的概念并不清楚,因此自己也写一篇,能够让自己想清楚。我的最初目的是要写到NO SQL,因此这系列的文章主要是关注在 1.数据库索引结构、2.表联接、3.递归查询这几个点上。一、基本概念1.数据的读取页(page)是SQL SERVER可以读写的最小I/O单位。即使只需访问一行,也要把整个页加载到缓存之中,再从缓存中读取数据。物理读取是从磁盘上读取,逻辑读取是从缓存中读取。物理读取一页的开销要比逻辑读取一页的要大得多。2.表的组织方式表有两种组织方式,B树(B tree)或者堆(heap)。当在表上创建了一个聚集索引的时候,整个表数 阅读全文
-
《算法导论》CLRS算法C++实现(十二)P208 最长公共子序列LCS
摘要:给定两个序列X和Y,如果Z既是X的一个子序列又是Y的一个子序列,则称序列Z是X和Y的一个公共子序列。在最长公共子序列问题(LCS)中,给定了两个序列X=<x1,x2,…,xm>和Y=<y1,y2,…,yn>,希望找出X和Y的最大长度的公共子序列。最直观且容易想到的方法是枚举出X的所有子序列,然后逐一检查看其是否为Y的子序列,并随时记录所发现的最长子序列。这种方法的时间复杂度是指数级的,对于较长的序列来说是不实际的。LCS问题的最优子结构:若xm=yn,则zk=xm=yn且Zk-1是Xm-1和Yn-1的最长公共子序列;若xm≠yn且zk≠xm,则Z是Xm-1和Y的最长公共 阅读全文
-
k-nearest neighbor
摘要:推荐系统相关算法(1):SVD1. SVD简介假如要预测Zero君对一部电影M的评分,而手上只有Zero君对若干部电影的评分和风炎君对若干部电影的评分(包含M的评分)。那么能预测出Zero君对M的评分吗?答案显然是能。最简单的方法就是直接将预测分定为平均分。不过这时的准确度就难说了。而本文将介绍一种比这个最简单的方法要准上许多,并且也不算复杂的算法。 SVD(Singular Value Decomposition)的想法是根据已有的评分情况,分析出评分者对各个因子的喜好程度以及电影包含各个因子的程度,最后再反过来根据分析结果预测评分。电影中的因子可以理解成这些东西:电影的搞笑程度,电影的爱情 阅读全文
-
重读<算法导论>
摘要:重读<算法导论>记得多年前学数据结构的时候作为教材的延伸读物我拜读了<算法导论>,当时有选择的读了大约2/3的东西,只学理论和算法实在枯燥,看书之余动手写了其中的一些算法,但是由于实在不知道这些知识有什么用处,剩余的1/3大部分是一些具体的理论推导和稍微复杂的算法。那么,为什么近期又重读这本算法著作呢?首先就是我遇到问题了,在前一阵的工作中频繁出现红黑树的分析问题,我不得不根据红黑树的排列去查找STL Map中的内容,每次出现问题都要去内存中找出有用的状态。其次就是在研究V8的时候我觉得有必要系统的研究一下编译器实现,于是研究了一段时间的<龙书>,那段时间一 阅读全文
-
浅谈java中常见的排序
摘要:浅谈java中常见的排序 学过java的人都知道,排序这一部分初次接触感觉还是有点难以理解,很多地方也会用到。然而,在java中常见的排序方法:冒泡排序,选择排序,插入排序等等。下面就让我们一起揭开他们神秘的面纱。 冒泡排序:想学好排序,最重要的是要知道它们的实现的原理,我们可以分三步来说明冒泡排序的原理:下面以数组为例: 1、 逐一比较数组中相邻的两个元素,如果后面的数字小于前面的数字,就交换先后元素。2、 经过一个轮次的比较,一定有一个最大的排在最后的位置。 3、 每次比较剩下的元素,经过n-1次比较,可以实现排序。 简单说: 比较相邻元素,大的向后交换。根据这些原理我们就推算出循环的条件 阅读全文