forked from AmazingAng/WTF-Solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_InsertionSort.sol
67 lines (61 loc) · 1.52 KB
/
07_InsertionSort.sol
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract InsertionSort {
// if else
function IfElseTest(uint256 _number) public returns(uint256){
//if(条件){
// ...;
//}else{
// ...;
//}
}
// for loop
function ForLoopTest(uint256 i) public{
uint n = 1;
for(i = 0; i < n; i++){
// ...;
}
}
// while
function WhileTest(uint256 i) public{
uint n = 0;
while(i < n){
// ...;
}
}
// do-while
function DoWhileTest(uint256 i) public{
uint n = 0;
do{
// ...;
}while(i < n);
}
// 插入排序 错误版
function insertionSortWrong(uint[] memory a) public pure returns(uint[] memory) {
// note that uint can not take negative value
for (uint i = 1;i < a.length;i++){
uint temp = a[i];
uint j=i-1;
while( (j >= 0) && (temp < a[j])){
a[j+1] = a[j];
j--;
}
a[j+1] = temp;
}
return(a);
}
// 插入排序 正确版
function insertionSort(uint[] memory a) public pure returns(uint[] memory) {
// note that uint can not take negative value
for (uint i = 1;i < a.length;i++){
uint temp = a[i];
uint j=i;
while( (j >= 1) && (temp < a[j-1])){
a[j] = a[j-1];
j--;
}
a[j] = temp;
}
return(a);
}
}