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 Euclid(m, n)
// Computes gcd(m, n) by Euclid's algorithm
// Input: Two nonnegative, not-both-zero integers m and n
// Output: Greatest common divisor of m and n
while n ≠ do
r <- m mod n
m <- n
n <- r
return m
Algorithm Sieve(n) 
  // Implements the sieve of Eratosthenes
// Input: An integer n ≥ 2
// Output: Array L of all prime numbers less than or equal to n
for p <- to n do A[p] <- p
for p <- to ⌊√n⌋ do
      if A[p] ≠ 0 // p hasn't been eliminated on previous passes
        j <- p * p
       while j ≤ n do
          A[j] <- 0 // mark element as eliminated
           j <- j + p
  // copy the remaining elements of A to array L of the primes
  i <- 0
  for p <- 2 to n do
     if A[p] ≠ 0
       L[i] <- A[p]
       i <- i + 1
  return L

Euclid's algorithm, as presented in Euclid's treatise,  uses subtractions rather than integer divisions. Write a pseudocode for this version of Euclid's Algorithm.  Here is a nonrecursive version:

Algorithm Euclid2(m, n)
// Computes gcd(m, n) by Euclid's algorithm based on subtractions
// Input: Two nonnegative interges m and n not both equal to 0
// Output: The greatest common divisor of m and n
while n ≠ do
if m < n swap(m, n)
m <- m - n
return m

Write a pseudocode for an algorithm for finding real roots of equation ax^2 + bx + c = 0 for arbitrary real coefficients a, b and c.(You may assume the availability of the square root function sqrt(x).)

Algorithm Quadratic(a, b, c)
// The algorithm finds real roots of equation ax^2 + bx + c = 0
// Input: Real coefficients a, b, c
// Output: The real roots of the equation or a message about their absence
if a ≠
D <- b*b - *a*c
if D >
temp <- *a
x1 <- (-b + sqrt(D)) / temp
x2 <- (-b - sqrt(D)) / temp
return x1, x2
else if D =
return -b / (*a)
else
return 'no real roots'
else // a = 0
if b ≠ return -c / b
else // a = b = 0
if c = return 'all real numbers'
else return 'no real roots'

Write a pseudocode to describe the standard algorithm for finding the binary representation of a positive decimal integer

Algorithm Binary(n)
// The algorithm implements the standard method for finding
//     the binary expansion of a positive decimal integer
   // Input: A positive decimal integer n
// Output: The list b(k), b(k-1)..., b(1), b(0) of n's binary digits
k <-
while n ≠
bk <- n mod
n <- ⌊n/2⌋
k <- k +

The following algorithm for finding the distance between the two closest elements in an array of numbers.

Algorithm MinDistance(A[..n-])
// Input: An array A[0..n-1] of numbers
// Output: The minimum distance d between two of its elements
dmin <- ∞
for i <- to n- do
for j <- i+ to n- do
temp <- |A[i] - A[j]|
if temp < dmin
dmin <- temp
return dmin

Consider the algorithm for the sorting problem that sorts an array by counting, for each of its elements, the number of smaller elements and then uses this information to put the elements in ins appropriate position in the sorted array

Algorithm ComparisionCountingSort(A[..n-], S[..n-])
// Sorts an array by comparison counting
// Input: Array A[0..n-1] of orderable values
// Output: Array S[0..n-1] of A's elements sorted in nondecreasing order
for i <- to n- do
Count[i] <-
for i <- to n- do
for j <- i+ to n- do
if A[i] < A[j]
Count[j] <- Count[j] +
else
Count[i] <- Count[i] +
for i <- to n- do
S[Count[i]] <- A[i]
New words:
indentation: 缩排 sieve: 筛子 Eratosthenes: a man_埃拉托色尼 treatise: 论文;专著
quadratic: 二次方程式

(End_xpjiang)

Design and Analysis of Algorithms_Introduction的更多相关文章

  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_Brute Froce

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

  4. 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 ...

  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. 【BZOJ】3834: [Poi2014]Solar Panels

    http://www.lydsy.com/JudgeOnline/problem.php?id=3834 题意:求$max\{(i,j)\}, smin<=i<=smax, wmin< ...

  2. Hbase1.0 客户端api

    最近在试用Hbase1.0的客户端API,发觉变化还是挺大(以前版本也不熟).到处都是deprecated. 现在应该是这样子: Configuration  conf = HBaseConfigur ...

  3. Handler消息传递机制

    引言: 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题. 为了解决这个问题,Android制定了一条简单的规则:只允许UI线程 ...

  4. python--range()函数

    1: >>> range(10, 0, -1) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] >>> range(10) [0, 1, 2, 3, ...

  5. Centos 下安装 文泉驿 字体 Odoo

    刚装完centos下的odoo的字体 文泉驿 ,一万头草泥马呼啸而过.....劝君如非必要,千万别再centos下折腾odoo..... 正题,文泉驿官网 只提供 deb包和源码包的字体安装 ,想在c ...

  6. bootstrap使用性能问题

    1.如icheckbox等的初始化,不要采用类似for in的循环来对所需应用的元素进行初始化,直接采用如  $('[data-toggle="popover"]').popove ...

  7. 【7集iCore3基础视频】7-4 iCore3连接示意图

    iCore3连接示意图: 高清源视频:链接:http://pan.baidu.com/s/1hr7ucpY%20密码:473n iCore3 购买链接:https://item.taobao.com/ ...

  8. PHP读文件的一个乱码问题

    D:/3.txt是utf-8文件 $f1 = fopen('D:/3.txt','r');$str = fread($f1,10000);fclose($f1);echo substr($str,1, ...

  9. js == 判断

    根据阮一峰介绍的算法文章分12部分可判断: 如果x不是正常值(比如抛出一个错误),中断执行. 如果y不是正常值,中断执行. 如果Type(x)与Type(y)相同,执行严格相等运算x === y. 如 ...

  10. ios-实现项目在开发、测试、正式环境快速部署

    快速部署:简单的来说,就是不用更改开发.测试.正式环境下的 url ,来实现在同一台测试手机快速部署三种项目状态. Bundle ID一样只会出现一个app:不一样会出现三个app: 具体步骤: 一. ...