-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
ReverseArray.sol
46 lines (41 loc) · 1.32 KB
/
ReverseArray.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title Array Reversal.
* @author [Sumit Banik](https://github.com/iSumitBanik)
* @dev Contract to demonstrate how an array is reversed.
*/
contract ReverseArray {
/**
* @dev internal function which takes in an array of numbers,start and end index,as an ouput returns the reversed array.
* @param _array => array of numbers
* @param start => start index of the array
* @param end => ending index of the array
* @return _array => reversed array.
*/
function _reverseArray(
uint256[] memory _array,
uint256 start,
uint256 end
) internal pure returns (uint256[] memory) {
while (start < end) {
(_array[start], _array[end]) = (_array[end], _array[start]);
start++;
end--;
}
return _array;
}
/**
* @dev external function which takes in an array of numbers and as an ouput returns the reversed array.
* @param _array => array of numbers
* @return _array => reversed array using the _reverseArray internal function.
*/
function reverse(uint256[] memory _array)
external
pure
returns (uint256[] memory)
{
uint256 end = (_array.length) - 1;
return _reverseArray(_array, 0, end);
}
}