知识库 知识库
首页
  • 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
      • Theory:Pseudocode basics
      • Theory:Complex constructions in pseudocode
        • Loops
        • Arrays
        • Functions
        • Implementing simple algorithms in pseudocode
        • Summary
      • 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:Complex constructions in pseudocode

On the previous steps, we started discussing pseudocode and covered some basic concepts, such as variables, arithmetic operations, conditional statements, and some others. However, it might not be enough: to describe some algorithms, we will need more complex constructions. In this topic, we will learn more advanced concepts used in pseudocode, such as loops, arrays, and functions. Knowing them will allow you to express sophisticated algorithmic ideas in a simple and concise manner.

# Loops

Loops serve to perform repeated calculations. We will use two kinds of loops: the while and the for loops. A while loop looks like this:

i = 0
while i < 5:
    print(i)
    i = i + 1
1
2
3
4

The syntax is the following: the while keyword followed by a condition, colon, and a loop body. This code means "execute a body while the condition is true". In this case, the snippet prints numbers from 0 to 4.

Here is what the for loop looks like:

sum = 0

for i in [1, 9]:
    sum = sum + i

print(sum) // 45, sum of numbers from 1 to 9
1
2
3
4
5
6

The [1, 9] construction denotes a range of numbers from 1 to 9. The last number is included in the range: we use a closed interval (opens new window) that includes all its limit points. In general, the for i in [a, b] means that the variable i is sequentially assigned to all numbers from the range [a, b].

# Arrays

Arrays serve to store a collection of objects of the same type. If we need an array and want to initialize its elements later, we will write the following construction :

array[1, 10] // 10-element array with indices from 1 to 10
1

Here, the variable array denotes an array of 10 elements. We can also initialize an array with some data explicitly:

fib = [0, 1, 1, 2, 3, 5, 8] // array with the name fib
1

The two most commonly-used operations for arrays are learning the length and accessing elements. Enumeration of elements starts with 1. As you may know, array indices in programming often start with 0, but we will use a common pseudocode approach. Let's have a look at how it works:

x = fib[4] // x is 2
length = len(fib) // length is 7

for i in [1, len(fib)]:
    print(fib[i])
1
2
3
4
5

The last for loop iterates through the numbers in the fib array and prints all of them to the screen.

Another useful operation is getting a subarray of an array. It functions as follows:

array = [0, 3, 2, 4, 1]
subarray = array[2, 4]
print(subarray) # 3, 2, 4
1
2
3

To get a subarray, we just specify the desired range in square brackets. Remember that the last number is included in the range.

# Functions

We will often work with functions, since they suit well our goal of ignoring the input format and cutting down on the code size. Now, let's learn how to write a function using pseudocode. Below is a function that calculates the mean value of numbers in an array:

function calc_mean(array):
    mean = 0

    for i in [1, len(array)]:
        mean = mean + array[i]
    
    return mean / len(array)
1
2
3
4
5
6
7

First, we put a function's name, then arguments in round brackets separated by spaces, after that an indent and a body. If we need to return something from a function, we use the return keyword, like in the example above.

# Implementing simple algorithms in pseudocode

Let's see how we can implement some simple algorithms using the described pseudocode. The first example is a function that takes an array of numbers as input and returns either zero (if the array is empty) or the maximum number in the array:

function find_max(array):
    if len(array) == 0 then
        return 0

    max = array[1]
    
    for i in [2, len(array)]:
        if array[i] > max then
            max = array[i]
    
    return max
1
2
3
4
5
6
7
8
9
10
11

Another example is a function that merges two arrays. It takes two sorted arrays as input and returns one sorted array containing the numbers from both input arrays:

function merge(left, right):
    merged[1, len(left) + len(right)] // new array 
    
    i = 1      // 
    j = 1      // indices for loop
    k = 1      // 
    
    // iterate over two arrays while we cannot use all elements from any array
    while i <= len(left) and j <= len(right):
        if left[i] < right[j] then    // put element from left array to merged array
            merged[k] = left[i]
            i = i + 1   // move to next element in left array
        else:
            merged[k] = right[j] // put element from right array to merged array
            j = j + 1   // move to next element in right array
        k = k + 1   // move to next element in merged array
                
    while i <= len(left):    // move remaining element in left array to merged array
        merged[k] = left[i]
        i = i + 1
        k = k + 1

    while j <= len(right):   // move remaining element in right array to merged array
        merged[k] = right[j]
        j = j + 1
        k = k + 1

    return merged
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

Note that we don't care about passing arguments by value, by reference, and so on. If you change any variable inside the function, those changes are saved outside the function. Thus, if you need to keep an argument immutable, just make a copy of it to operate with. Consider this example of the swap function that swaps two numbers:

function swap(a, b):
    temp = a
    a = b
    b = temp

c = 3
d = 5

swap(c, d)

print(c) // 5
print(d) // 3
1
2
3
4
5
6
7
8
9
10
11
12

# Summary

In this topic, we've learned some advanced concepts that we use in pseudocode: loops, arrays, and functions. Along with the ideas covered in the introductory part, they are enough to express both simple and complex algorithmic ideas in a clear manner. Further, we will use the introduced syntax to describe and learn algorithms. Remember: in our dialect, arrays starts with 1, and we use closed ranges!

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

← Theory:Pseudocode basics Theory:The big O notation→

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