I collect and make up this pseudocode from the book:

<<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany Levitin
Note that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no verfication. When implementing algorithms as programs to be used in actual applications, you should provide such verfications.
About pseudocode: For the sake of simplicity, we omit declarations of variables and use indentation to show the scope of such statements as for, if and while. As you saw later, we use an arrow <- for the assignment operation and two slashes // for comments. 

Algorithm SelectionSort(A[..n-])
// Sorts a given array by selection sort
// Input: An array A[0..n-1] of orderable elements
// Output: Array A[0..n-1] sorted in ascending order
for i <- to n- do
min <- i
for j <- i+ to n- do
if A[j] < A[min]
min <- j
swap A[i] and A[min]
Algorithm BubbleSort(A[..n-])
// Sorts a given array by bubble sort
// Input: An array A[0..n-1] of orderable elements
// Output: Array A[0..n-1] sorted in ascending order
for i <- to n- do
for j <- to n--i do
if A[j+] < A[j]
swap A[j] and A[j+]
Here is a pseudocode for the improved version of bubble sort:
Algorithm BetterBubbleSort(A[..n-])
// The algorithm sorts array A[0..n-1] by improved bubble sort
// Input: An array A[0..n-1] of orderable elements
// Output: Array A[0..n-1] sorted in ascending order
count <- n- // number of adjacent pairs to be compared
sflag <- true // swap flag
while sflage do
sflage <- false
for j <- to count- do
if A[j+] < A[j]
swap A[j] and A[j+]
sflag <- true
count <- count-


Here is a pseudocode of teh most straightforward version:
Algorithm BruteForcePolynomialEvaluation(P[..n], x)
// The algorithm computes the value of polynomial P at a given point x by the
//     "highest-to-lowest" brute-force algorithm
// Input: Array P[0..n] of the coefficients of a polynomial of degree n, stored from the lowest
//     to the highest and a number x
// Output: The value of the polynomial at the point x
p <- 0.0
for i <- n downto do
power <-
for j <- to i do
power <- power*x
p <- p + P[i]*power
return p
We can count just the number of multiplications in the algorithm's inner-most loop to find the algorithm's efficiency class: M(n) = n(n+1)/2 = O(n^2)
The above algorithm is very inefficient: we recompute powers of x again and again as if there were no relationship among them. Thus, the obvious improvement is based on computing consecutive powers more efficiently:
Algorithm BetterBruteForcePolynomialEvaluation(P[..n-], x)
// The algorithm computes the value of polynomial P at a given point x by the
//     "lowest-to-highest term" algorithm
// Input: Array P[0..n] of the coefficients of a polynomial of degree n, from the
//     lowest to the highest, and a number x
// Output: The value of the polynomial at the point x
p <- P[]; power <-
for i <- to n do
power <- power*x
p <- p + P[i]*power
return p
The number of multiplications here is M(n) = 2n, in another word, we have a linear algorithm
Algorithm SequentialSearch2(A[..n], K)
// Implements sequential search with a search key as a sentinel
// Input: An array A of n elements and a search key K
// Output: The index of the first element in A[0..n-1] whose value is equal
// to K or -1 if no such element if found
A[n] <- K
i <-
while A[i] ≠ K do
i <- i +
if i < n return i
else return - Algorithm BruteForceStringMatch(T[..n-], P[..m-])
// Implements brute-force string matching
// Input: An array T[0..n-1] of n characters representing a text and
// an array P[0..m-1] of m characters representing a pattern
// Output: The index of the first character in the text that starts a
// matching substring or -1 if the search is unsuccessful
for i <- to n-m do
j <-
while j < m and P[j] = T[i+j] do
j <- j+
if j = m return i
return -
Algorithm BruteForceClosestPoints(P)
// Finds two closest points in the plane by burte force
// Input: A list P of n(n ≥ 2) points P1 = (x1, x2),...,Pn = (xn, yn)
// Output: Indices index1 and index2 of the closest pair of points
dmin <- ∞
for i <- to n- do
for j <- i+ to n do
d <- sqrt((xi-xj) + (yi-yj)) // sqrt is the square root function. In fact, computing square roots can be avoided, the trick is to realize that we can simply ignore the square root function
if d < dmin
dmin <- d; index1 <- i; index2 <- j
return index1, index2
Let x1 < x2 < ... < xn be real numbers representing coordinates of n villages located along a straight road. A post office needs tobe built in one of these villages. Design an efficent algorithm to find the post offfice location minimizing  the maximum distance from a village to the post office.
Assuming that the points x1, x2, ... xn are given in increasing order, the answer is the point xi that is the closest to m = (x1 + xn) / 2, the middle point between x1 and xn. (The middle point woule be the obvious solution if the post-post office didn't have tobe at one of the given locations.) Indeed, if we put the post office at any location xi to the left of m, the longest distance froma village to the post office would be xn - xi; this distance is minimal for the rightmost among such points. If we put the post office at any location xi to the right of m, the longest distance from a village to the post office would be xi - x1; this distance isminimal for the leftmost among such points.
Algorithm PostOffice(P)
// Input: List P of n(n ≥ 2) point s x1, x2,..., xn in increasing order
// Output: Point xi that minimizes max(1≤j≤n)|xj - xi| among all x1,x2,...,xn
m <- (x1+xn) /
i <-
while xi < m do
i <- i+
if xi - x1 < xn - xi-
return xi
else return xi-
new words:
polynomial: 多项式 coefficient: 系数 inefficient: 低效的
sentinel: 哨兵 plane: 平面 coordinate: 坐标
village: 村庄 straight: 直; 直线 (END_XPJIANG)

Design and Analysis of Algorithms_Brute Froce的更多相关文章

  1. Design and Analysis of Algorithms_Decrease-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  2. Design and Analysis of Algorithms_Divide-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  3. Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  4. Design and Analysis of Algorithms_Introduction

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  5. 6.046 Design and Analysis of Algorithms

    课程信息 6.046 Design and Analysis of Algorithms

  6. 斯坦福大学公开课机器学习: machine learning system design | error analysis(误差分析:检验算法是否有高偏差和高方差)

    误差分析可以更系统地做出决定.如果你准备研究机器学习的东西或者构造机器学习应用程序,最好的实践方法不是建立一个非常复杂的系统.拥有多么复杂的变量,而是构建一个简单的算法.这样你可以很快地实现它.研究机 ...

  7. Algorithms: Design and Analysis, Part 1 - Programming Assignment #1

    自我总结: 1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合 2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好 题目: In this programming ass ...

  8. Algorithms: Design and Analysis, Part 1 - Problem Set 1 - Question 5

    最后一个图像,用画图软件绘制了一下,自己的直接主观判断还是有些小问题的 注意:最后的灰色的线条会超过橙色的线条

  9. EE就业最好的方向是转CS,其次是VLSI/ASIC DESIGN & VERIFICATION

    Warald在2012年写过一篇文章<EE现在最好就业的方向是VLSI/ASIC DESIGN VERIFICATION>,三年过去了,很多学电子工程的同学想知道现在形势如何. 首先,按照 ...

随机推荐

  1. Unity3d与Android交互

    先看下效果 你一定会说,然并卵! 没错,这里只是一个最简单的例子,unity与android activity 互相传参数. 玩过手游的都知道,在你要为你心爱的游戏角色准备花钱买钻石,点击购买的时候, ...

  2. 第三方登录分享功能-ShareSDK for iOS适配问题记录

    最近app里需要添加第三方授权登陆和分享的功能,选择了ShareSDK,参考了ShareSDK文档对该SDK进行了适配,但遇到了一些问题 1.问题:分享功能点击不跳转  原因:适配iOS9添加白名单 ...

  3. 最简单的html轮播图制作适合新手

    html代码 --------------------------------------------------------------------------------------------- ...

  4. 开源的一些GIS项目下载

    worldwind svn地址: https://nasa-exp.svn.sourceforge.net/svnroot/nasa-exp/trunk/WorldWind dotspatial sv ...

  5. Learn ZYNC (5)

    今天为了熟悉axiLite的自定义ip核设计, 把LED和SW的往AXI总线输入输出定义在一个ip核中, BD设计如下: ip核顶层文件(增加了LED_Out和SW_In的定义)mygpio_v1.0 ...

  6. jquery 时间戳与日期转换

    (function($) { $.extend({ myTime: { /** * 当前时间戳 * @return <int> unix时间戳(秒) */ CurTime: functio ...

  7. vm安装centos 老是出现 grub.conf 配置问题

    vm 环境 11  centos 6.5 最开始用的是vm12 发现安装软件一会就出现 客户机操作系统已禁用 cpu.请关闭或重置虚拟机 以为是新机器的cpu或者主板有问题,换vm,换系统依然会出现这 ...

  8. 一些稍微复杂点的sql语句

    UPDATE test SET content = REPLACE(content,'国家级',''),content = REPLACE(content,'世界级',''),content = RE ...

  9. canvas绘制二次贝塞尔曲线----演示二次贝塞尔四个参数的作用

    canvas中绘制二次贝塞尔曲线的方法为ctx.quadraticCurveTo(x1,y1,x2,y2); 四个参数分别为两个控制点的坐标.开始点即当前canvas中目前的点,如果想从指定的点开始, ...

  10. C#枚举类型

    枚举是一个指定的常数,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32. 定义 默认基数从0开始,也可指定数值. enum Days { Saturday, / ...