header-langage
简体中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

Simple Introduction to Solidity|Lecture 10: Control Flow, Using Solidity to Implement Insertion Sort

Read this article in 9 Minutes
This article introduces the control flow in solidity, and writes insertion sort in solidity.
Original title: " A minimalist introduction to Solidity: 10.  Control flow, use solidity to implement insertion sort "
Original author: 0xAA  


I am re-learning solidity recently, consolidate the details, and write a "Solidity minimalist "Getting Started" is for novices (programmers can find other tutorials), 1-3 lectures are updated every week.


All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity


Here In the first lecture, we will introduce the control flow in solidity, and then talk about how to use solidity to implement insertion sort (InsertionSort), a program that looks simple but is actually easy to write bugs.


Control flow


Solidity's control flow is similar to other languages, mainly including the following :


if-else  



for loop < /b>

 


while loop  



do-while Loop  



Ternary Operator


Ternary The operator is the only operator in solidity that accepts three operands, the rule condition ? condition is true expression: the condition is false expression. This operator is often used as a shortcut for if statements.   



< p> In addition, there are continue (immediately enter the next loop) and break (jump out of the current loop) keywords can be used.


Using solidity to achieve insertion sort


Write in front:


More than 90% of people who use solidity to write insertion algorithms will make mistakes. Insertion sort


The sorting algorithm solves the problem of arranging an unordered set of numbers, such as [2, 5, 3, 1], from small to large . Insertion sort (InsertionSort) is the simplest sorting algorithm, and it is also the first algorithm that many people learn. Its idea is very simple. From front to back, compare each number with the number in front of it in turn. If it is smaller than the number in front of it, swap the positions. Schematic diagram:


  insertion sort  


python code


We can first look at the python code of insertion sort:

< p>


There is a BUG after rewriting into solidity!


A total of 8 lines of python code can complete the insertion sort, which is very simple. Then we rewrite it into solidity code, convert functions, variables, loops, etc., and only need 9 lines of code:



Then let's put the modified one on the remix and run it, input [2, 5, 3, 1] . BOOM! There are bugs! After changing it for a long time, I couldn't find where the bug is. I went to Google to search for "solidity insertion sort", and found that the insertion algorithm tutorials written in solidity on the Internet are all wrong, such as: Sorting in Solidity without Comparison


Correct solidity insertion sort


It took several hours, with the help of a friend in the Dapp-Learning community, I finally found it where the bug is. The most commonly used variable type in solidity is uint, which is a positive integer. If a negative value is obtained, an underflow error will be reported. In the insertion algorithm, the variable j may take -1, causing an error.


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



The result after running:



Summary


< p>In this lecture, we introduced the control flow in solidity, and wrote insertion sort in solidity. It looks easy, but it's really hard. This is solidity, and there are many pitfalls. Every month, projects lose tens of millions or even hundreds of millions of dollars because of these small bugs. Only by mastering the basics and practicing constantly can you write better solidity code.


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit