Solidity Minimalist Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

In this lecture, we will introduce the control flow in solidity, and then talk about how to implement InsertionSort with solidity, a program that looks simple but is actually easy to write bugs.

I’ve recently been relearning solidity, consolidating the details, and writing a “Primer to Solidity” for the little ones to use (programming gurus can find another tutorial), updated 1-3 times a week.

All code and tutorials are open source in github: github.com/AmazingAng/WTFSolidity

In this lecture, we will introduce the control flow in solidity, and then talk about how to implement InsertionSort with solidity, a program that looks simple but is actually easy to write bugs.

control flow

Solidity’s control flow is similar to other languages and consists mainly of the following:

if-else

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

for loop

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

while looping

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

do-while loop

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

Ternary operator

The ternary operator is the only operator in solidity that accepts three operands. Expression with true condition: An expression whose condition is false. This operator is often used as a shortcut to the if statement.

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

There are also continue (immediately enter the next loop) and break (jump out of the current loop) keywords can be used.

Insert sorting is implemented with solidity

Write on the front:

More than 90% of people with solidity write-insert algorithms will make mistakes. Insert sorting

The problem solved by the sorting algorithm is to arrange an unordered set of numbers, such as [2, 5, 3, 1], from small to large. InsertionSort is the simplest sort algorithm and the first algorithm many people learn. Its idea is very simple, from front to back, in turn, each number and the number in front of him are proportional to the size, if it is smaller than the previous number, the position is swapped. Diagram:

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

Insert sorting

python code

We can first look at the python code that inserts the sort:

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

Rewrite to solidity after BUG!

A total of 8 lines of python code can complete the insertion sorting, very simple. Then we rewrite it into solidity code, converting functions, variables, loops, etc. accordingly, which only takes 9 lines of code:

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

So let’s put the modified one on the remix and enter [2, 5, 3, 1]. BOOM! There is a bug! Changed for half a day, did not find where the bug is. I googled “solidity insertion sort” again, and found that the insertion algorithm tutorials written in solidity on the Internet were all wrong, such as: Sorting in Solidity without Comparison

Correct solidity insertion sorting

It took several hours, with the help of a friend in the Dapp-Learning community, to finally find the problem. The most commonly used variable type in solidity is uint, which is a positive integer, and if a negative value is taken, it will report an underflow error. In the insertion algorithm, the variable j may take -1, causing an error.

Here, we need to add j to 1 so that it cannot take negative values. Correct code:

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

Results after running:

Introduction to Solidity Lecture 10: Control Flow, Implementing Insert Sorting with Solidity

summary

In this lecture, we introduced the flow of control in solidity and wrote the insertion sort in solidity. It looks simple, but it’s actually hard. This is solidity, there are many pits, and every month there are projects that lose tens of millions or even hundreds of millions of dollars because of these small bugs. Master the basics and practice to write better solidity code.

Posted by:CoinYuppie,Reprinted with attribution to:https://coinyuppie.com/solidity-minimalist-lecture-10-control-flow-implementing-insert-sorting-with-solidity/
Coinyuppie is an open information publishing platform, all information provided is not related to the views and positions of coinyuppie, and does not constitute any investment and financial advice. Users are expected to carefully screen and prevent risks.

Like (0)
Donate Buy me a coffee Buy me a coffee
Previous 2022-10-03 09:06
Next 2022-10-03 09:09

Related articles