Design and Analysis of Algorithms_Introduction

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 ≠ 0 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 <- 2 to n do A[p] <- p for p <- 2 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 ≠ 0 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 ≠ 0
        D <- b*b - 4*a*c
        if D > 0
            temp <- 2*a
            x1 <- (-b + sqrt(D)) / temp
            x2 <- (-b - sqrt(D)) / temp
            return x1, x2
        else if D = 0
            return -b / (2*a)
        else
            return 'no real roots'
    else // a = 0
        if b ≠ 0 return -c / b
        else // a = b = 0
            if c = 0 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 <- 0 while n ≠ 0 bk <- n mod 2 n <- ⌊n/2⌋
k
<- k + 1

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

Algorithm MinDistance(A[0..n-1])
    // Input: An array A[0..n-1] of numbers
    // Output: The minimum distance d between two of its elements
    dmin <-for i <- 0 to n-1 do
        for j <- i+1 to n-1 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[0..n-1], S[0..n-1])
    // 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 <- 0 to n-1 do
        Count[i] <- 0
    for i <- 0 to n-2 do
        for j <- i+1 to n-1 do
            if A[i] < A[j]
                Count[j] <- Count[j] + 1
            else
                Count[i] <- Count[i] + 1
    for i <- 0 to n-1 do
        S[Count[i]] <- A[i]
New words:
indentation: 缩排 sieve: 筛子 Eratosthenes: a man_埃拉托色尼 treatise: 论文;专著
quadratic: 二次方程式

 (End_xpjiang)

转载于:https://www.cnblogs.com/xpjiang/p/4524186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值