I'm trying to have a simple array of integer sorted in Solidity but i couldn't find any real ressources so instead i'm trying to do it "the hard way" but so far with very little success.
Is anyone aware of anything that could help ?
This is what i've tried so far , but with no luck. Whenever i try it i'm getting out of memory errors.
function quickSort(uint[] arr, uint left, uint right) private returns(uint[] _arr){
uint i = left;
uint j = right;
uint tmp;
uint pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
Anyone got something to share ?
Error is often " ERROR: invalid jump destination (PUSH1) "
Edit :
Same with :
function insertionSort(uint[] a){
for (uint i = 1;i < a.length;i++){
uint temp = a[i];
uint j;
for (j = i -1; j >= 0 && temp < a[j]; j--)
a[j+1] = a[j];
a[j + 1] = temp;
}
}
edit 2 :
Fixed aswell
function insertionSort(uint[] a)internal {
for (uint i = 1;i < a.length;i++){
uint temp = a[i];
uint j;
for (j = i -1; j >= 0 && temp < a[j]; j--)
a[j+1] = a[j];
a[j + 1] = temp;
}
}