知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

    • Java basic

    • Java OOP

    • 应知

    • 扩展

    • IO & Stream

    • Error & Exception

    • Algorithm & Data structure

      • Theory:Computer algorithms
      • Theory:Pseudocode
        • What is pseudocode?
        • Why do we need pseudocode?
        • Pseudocode example
        • Conclusion
      • Theory:Pseudocode basics
      • Theory:Complex constructions in pseudocode
      • Theory:The big O notation
      • Theory:Best, Average and cases
      • Theory:Data structures
      • Theory:Fixed-size array
      • Theory:Dynamic array
    • Design pattern

    • Web

    • Spring boot

  • 练习题

  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • Hyperskill - Java
  • Algorithm & Data structure
Jim
2022-09-18
目录

Theory:Pseudocode

Different people use different programming languages, and that often becomes a problem. If you implement an algorithm you've written in one particular language, developers who don't know that language would hardly be able to understand it. To solve this problem, you can use a pseudocode, a special artificial language that stands somewhere in between "human" language and code. Let's find out what it is and why we need it at all.

# What is pseudocode?

Despite the variety of programming languages, they all share some common features. These include variables, loops, if-else blocks, and so on. In fact, if we remove all language-specific features from a program, we are left with its "logical core", which is the essence of any algorithm. By isolating this core, we are inevitably forced to resort to high-level constructs like "Do A until B happens", where both A and B can be quite complex operations. So, this essence of an algorithm may be called a pseudocode**.**

If we decide to use pseudocode, we lose the opportunity to "explain" our instructions to a computer, which requires a significantly lower-level input. On the other hand, we gain a lot in the brevity of our solution and its comprehensibility to people, which is exactly what we strive for when we create pseudocode.

# Why do we need pseudocode?

But why should we use an abstract language, not an existing one? Of course, we can use a popular and simple language like Python, and many programmers can understand this code. The main problem here is that in real code you need to work with memory, include libraries, solve some problems with visibility, variable types, and so on. Why do we need to think about this stuff if we want to understand the algorithm? An abstract language better describes the idea of an algorithm without complications.

Another obvious solution to the problem of universal description of an algorithm is to simply describe it in human language. Alas, this is also a bad idea. In this case, you have to read a lot of text and take some time to figure out what the code will look like. With pseudocode, you don't need to clarify the description, and it's easy to see the structure of the code.

# Pseudocode example

Let's solve a standard task and find the maximum value in an array of positive numbers. The array is just an ordered bunch of numbers if you're not already familiar with the term.

First, let's look at a pseudocode function:

function max(array):            // you receive an array somehow
    if len(array) == 0 then     // compare the size of array with 0
        return -1               // empty array, no maximum

    max = 0                     // assume that maximum is the 0
    
    for i in [1, len(array)]:    // iterate over the array, array indices start at 1
        if array[i] > max then  // if we find something greater, we change the maximum
            max = array[i]
    
    return max                 // our result
1
2
3
4
5
6
7
8
9
10
11

It looks pretty straightforward and gives a sense of the algorithm's internal logic.

Now let's look at the Python code that does basically the same:

n = int(input())                # the size of array 
array = []                      # empty array
for i in range(n):              # do something n times
    array.append(int(input()))  # add element to the array

if n == 0:                      # empty array
    print(-1)

else:
    max = 0                     # current maximum

    for i in array:             # iterate over the array
        if i > max:         
            max = i             # update the maximum

    print(max)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

As you can see, we can omit reading and storing values. With pseudocode, we can describe only the algorithm's logic.

The pseudocode has a lot of variations and dialects. In this course, we will use a specific version of pseudocode. We will talk more about our dialect in the following topics. For now, it's important that you know how to read and understand pseudocode, not how to write it correctly.

# Conclusion

Pseudocode is a way of expressing an algorithm without following fixed syntax rules*.* It is widely used to communicate the essence of an algorithm to others while ignoring the details of its implementation. With pseudocode, you can easily communicate ideas and concepts to other developers, no matter what language they write in. Pseudocode has many dialects, but in this course we will use a specific version that we'll discuss later.

编辑 (opens new window)
#Algorithm
上次更新: 2022/09/26, 16:55:15
Theory:Computer algorithms
Theory:Pseudocode basics

← Theory:Computer algorithms Theory:Pseudocode basics→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Processing strings
02-18
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式