Created
February 3, 2023 13:49
-
-
Save gsscoder/24e09900a30c7c057bbd5bc2dfb9219f to your computer and use it in GitHub Desktop.
Bubble sort implementation in Solidity
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
contract BubbleSort { | |
function sort(uint[] memory array) public pure returns (uint[] memory) { | |
bool swapped; | |
for (uint i = 1; i < array.length; i++) { | |
swapped = false; | |
for (uint j = 0; j < array.length - i; j++) { | |
uint next = array[j + 1]; | |
uint actual = array[j]; | |
if (next < actual) { | |
array[j] = next; | |
array[j + 1] = actual; | |
swapped = true; | |
} | |
} | |
if (!swapped) { | |
return array; | |
} | |
} | |
return array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment