Skip to content

Instantly share code, notes, and snippets.

@an-ivannikov
Forked from subhodi/QuickSort.sol
Created March 5, 2023 22:45
Show Gist options
  • Select an option

  • Save an-ivannikov/ead9f297e1fa16cbf69573daeae29e57 to your computer and use it in GitHub Desktop.

Select an option

Save an-ivannikov/ead9f297e1fa16cbf69573daeae29e57 to your computer and use it in GitHub Desktop.
Quick sort in Solidity for Ethereum network
pragma solidity ^0.4.18;
contract QuickSort {
function sort(uint[] data) public constant returns(uint[]) {
quickSort(data, int(0), int(data.length - 1));
return data;
}
function quickSort(uint[] memory arr, int left, int right) internal{
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment