Skip to content

Instantly share code, notes, and snippets.

@gsscoder
Created February 3, 2023 13:49
Show Gist options
  • Save gsscoder/24e09900a30c7c057bbd5bc2dfb9219f to your computer and use it in GitHub Desktop.
Save gsscoder/24e09900a30c7c057bbd5bc2dfb9219f to your computer and use it in GitHub Desktop.
Bubble sort implementation in Solidity
// 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