solidityでbubblesort
solidityは破壊的関数が実装できるようだ。Javaと一緒。
https://ethfiddle.com/18YvWzBm06
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 |
//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.18; contract SimpleStore { function getHashArray() public view returns(uint[]){ bytes32 hash = keccak256(now); uint[] memory ret = new uint[](32); for(uint i=0; i<32; i++){ ret[i] = uint(hash[i]); } return ret; } function swap(uint[] a, uint l, uint r) private pure { uint t = a[l]; a[l] = a[r]; a[r] = t; } function bubblesort(uint[] a) private pure { uint len = a.length; for(uint i=0; i<len-1; i++){ for(uint j=i; j<len; j++){ if(a[j] < a[i]){ swap(a, i, j); } } } } function get() public view returns(uint[]){ uint[] memory a = getHashArray(); bubblesort(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 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 |
getHashArray [ "248", "226", "185", "19", "94", "22", "14", "128", "144", "188", "151", "237", "200", "124", "90", "47", "161", "113", "146", "192", "67", "54", "203", "213", "189", "193", "192", "100", "95", "46", "103", "161" ] get [ "14", "19", "22", "46", "47", "54", "67", "90", "94", "95", "100", "103", "113", "124", "128", "144", "146", "151", "161", "161", "185", "188", "189", "192", "192", "193", "200", "203", "213", "226", "237", "248" ] |