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

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

Read this article in 8 Minutes
This article will introduce the control flow in Solidity and how to use Solidity to implement insertion sort (InsertionSort).
Original title: "Introduction to Solidity Minimalist: 7.  Control flow, use solidity to implement insertion sorting"
Original author: 0xAA


I am re-learning solidity recently, to consolidate the details, and also write a "Solidity minimalist introduction" for use by novices (Programmers can find another tutorial), 1-3 lectures are updated every week.


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


In this lecture, we will introduce the control flow in solidity, and then talk about how to use solidity to implement insertion sort (InsertionSort), a seemingly simple, but practical It is easy to write buggy programs.


Control flow


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


if-else


< br>

for loop



while Loop



do-while loop



Ternary operator


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



In addition, there are continue (immediately enter the next cycle) and The break (out of the current loop) keyword can be used.


Using solidity to achieve insertion sort


Written in the front: more than 90% People who use solidity to write insertion algorithms will make mistakes.


Insertion sort


The sorting algorithm solves the problem of An unordered set of numbers, such as [2, 5, 3, 1], is arranged in order 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. Diagram:


insertion sort  ;


python code


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



There is a bug after rewriting to 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, for example: Sorting in Solidity without Comparison


Correct


Spent several hours, with the help of a friend in the Dapp-Learning community, I finally found the bug. 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