solidityでquicksort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
pragma solidity ^0.4.18; contract SimpleStore { function getRandomHash() public view returns(bytes32) { return keccak256(now); } function getRandomIntArray() public view returns(uint[]) { uint[] memory a = new uint[](32); bytes32 hash = getRandomHash(); for(uint i=0; i<32; i++){ a[i] = uint(hash[i]); } return a; } function getPivot(uint a, uint b, uint c) public pure returns(uint) { if(a>b){ if(b>c){ return b; }else{ return a>c ? c:a; } }else{ if(a>c){ return a; }else{ return b>c ? c:b; } } } function swap(uint[] a, uint l, uint r) public pure { uint t = a[l]; a[l] = a[r]; a[r] = t; } function quicksort(uint[] a) public pure { uint left = 0; uint right = a.length - 1; quicksort_core(a, left, right); } function quicksort_core( uint[] a, uint left, uint right ) public pure { if(right <= left){ return; } uint l = left; uint r = right; uint p = getPivot(a[l], a[l+1], a[r]); while(true){ while(a[l] < p){ l++; } while(p < a[r]){ r--; } if(r<=l){ break; } swap(a, l, r); l++; r--; } quicksort_core(a, left, l-1); quicksort_core(a, r+1, right); } function get() public view returns(uint[]) { uint[] memory a = getRandomIntArray(); quicksort(a); return a; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
function get - [ "2", "5", "9", "23", "32", "33", "33", "40", "43", "54", "58", "72", "80", "90", "91", "94", "95", "101", "107", "123", "135", "135", "144", "153", "156", "164", "164", "187", "221", "232", "246", "249" ] |