forked from mejtfk/Algorithms-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort-array.sol
More file actions
30 lines (26 loc) · 776 Bytes
/
sort-array.sol
File metadata and controls
30 lines (26 loc) · 776 Bytes
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
pragma solidity >=0.4.24 <0.6.0;
contract SortArray {
struct Items {
uint[] items;
}
function sort(uint[] storage items, bool desc) private returns (uint[] memory) {
uint length = items.length;
uint[] storage arr = items;
for (uint i = 0; i < length; i++) {
for (uint j = i + 1; j < length; j++) {
bool condition;
if (desc) {
condition = arr[i] < arr[j];
} else {
condition = arr[i] > arr[j];
}
if (condition) {
uint temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
return arr;
}
}