# 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

for loop

while looping

do-while loop

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.

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:

Insert sorting

python code

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

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:

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:

Results after running:

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

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)
Previous 2022-10-03 09:06
Next 2022-10-03 09:09

2022-05-12

2022-01-12

2021-05-06

2021-06-08

2021-12-17

2021-12-13