solidityでクイックソート~フロントエンドを書いてgithubを使った~
https://yuzunoha.github.io/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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
pragma solidity ^0.4.24; contract QuickSort { mapping(address => uint[]) private mOwnArray; event QuickSortCompleted( address indexed who, uint length, uint timestamp ); event SetRandomCompleted( address indexed who, uint length, uint timestamp ); function _getPivot(uint a, uint b, uint c) private 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) private pure { uint t = a[l]; a[l] = a[r]; a[r] = t; } function _quicksort_core(uint[] a, uint left, uint right) private 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 _quicksort(uint[] a) private pure returns(uint[]) { _quicksort_core(a, 0, a.length - 1); return a; } function _bytes32ArrayToUintArray(bytes32[] _bytes32Array) private pure returns(uint[]) { uint lenIn = _bytes32Array.length; uint lenOut = lenIn * 32; uint[] memory ret = new uint[](lenOut); uint cnt = 0; for(uint i = 0; i < lenIn; i++){ for(uint j = 0; j < 32; j++){ ret[cnt++] = uint(_bytes32Array[i][j]); } } return ret; } function _getRandomUintArray(uint len) private view returns(uint[]) { uint nowLocal = now; address msgSender = msg.sender; uint times = 1 + uint(len / 32); bytes32[] memory bytes32Array = new bytes32[](times); uint i; for(i = 0; i < times; i++){ uint seed = i + 1234567890; bytes32Array[i] = keccak256(seed, nowLocal, msgSender); } uint[] memory buf = _bytes32ArrayToUintArray(bytes32Array); uint[] memory ret = new uint[](len); for(i = 0; i < len; i++){ ret[i] = buf[i]; } return ret; } function getMyArray() external view returns(uint[]) { return mOwnArray[msg.sender]; } function setRandom(uint len) external { uint[] storage mArray = mOwnArray[msg.sender]; mArray.length = 0; uint[] memory randomUintArray = _getRandomUintArray(len); for(uint i = 0; i < len; i++){ mArray.push(randomUintArray[i]); } emit SetRandomCompleted(msg.sender, len, now); } function quicksort() external { address msgSender = msg.sender; uint[] storage mArray = mOwnArray[msgSender]; uint len = mArray.length; uint[] memory b = new uint[](len); uint i; for(i = 0; i < len; i++){ b[i] = mArray[i]; } _quicksort(b); for(i = 0; i < len; i++){ mArray[i] = b[i]; } emit QuickSortCompleted(msgSender, len, now); } } |