-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
function quickSort(uint[] memory arr, int left, int right) pure { | ||
int i = left; | ||
int j = right; | ||
if (i == j) return; | ||
uint pivot = arr[uint(left + (right - left) / 2)]; | ||
while (i <= j) { | ||
while (arr[uint(i)] < pivot) i++; | ||
while (pivot < arr[uint(j)]) j--; | ||
if (i <= j) { | ||
(arr[uint(i)], arr[uint(j)]) = (arr[uint(j)], arr[uint(i)]); | ||
i++; | ||
j--; | ||
} | ||
} | ||
if (left < j) | ||
quickSort(arr, left, j); | ||
if (i < right) | ||
quickSort(arr, i, right); | ||
} | ||
|
||
contract QuickSort { | ||
function sort(uint[] memory data) public pure returns (uint[] memory) { | ||
quickSort(data, int(0), int(data.length - 1)); | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
import {QuickSort} from "../src/Sort.sol"; | ||
import { console } from "forge-std/console.sol"; | ||
|
||
contract SortTest is Test { | ||
QuickSort public quickSort; | ||
|
||
function setUp() public { | ||
quickSort = new QuickSort(); | ||
} | ||
|
||
function testSort() public { | ||
for (uint256 i = 10; i <= 400; i+=10) { | ||
runSort(i); | ||
} | ||
} | ||
|
||
function runSort(uint256 size) public { | ||
uint[] memory data = new uint[](size); | ||
for (uint256 i = 0; i < size; i++) { | ||
data[i] = i % 3; | ||
} | ||
|
||
uint256 gas = gasleft(); | ||
quickSort.sort(data); | ||
console.log("Gas (in dollars) used for array of size ", size, " gas: ", gas - gasleft()); | ||
} | ||
|
||
function gasToDollars(uint256 gas) internal pure returns (uint256) { | ||
return gas * 1e9 * 30000 / 1e18; | ||
} | ||
} |