|
| 1 | +import Sort from '../Sort'; |
| 2 | +import Comparator from '../../../utils/comparator/Comparator'; |
| 3 | +export default class BCIS extends Sort { |
| 4 | + /** |
| 5 | + * @param {Object[]} _array array to be swapped |
| 6 | + * @param {number} a index a to be swapped with index b |
| 7 | + * @param {number} b index b to be swapped with index a |
| 8 | + */ |
| 9 | + static SWAP(_array = [], i, j) { |
| 10 | + const temp = _array[i]; |
| 11 | + _array[i] = _array[j]; |
| 12 | + _array[j] = temp; |
| 13 | + } // end SWAP |
| 14 | + /** |
| 15 | + * @param {Object[]} _array |
| 16 | + * @param {*} _current_item |
| 17 | + * @param {number} SR |
| 18 | + * @param {number} right |
| 19 | + */ |
| 20 | + static INSRIGHT(_array, _current_item, SR, right, comp) { |
| 21 | + let j = SR; |
| 22 | + do { |
| 23 | + _array[j - 1] = _array[j]; |
| 24 | + j = j + 1; |
| 25 | + } |
| 26 | + while (comp.lessThanOrEqual(j,right) && comp.greaterThan(_current_item, _array[j])); |
| 27 | + _array[j - 1] = _current_item; |
| 28 | + } // end INSRIGHT |
| 29 | + /** |
| 30 | + * |
| 31 | + * @param {Object[]} _array |
| 32 | + * @param {*} _current_item |
| 33 | + * @param {number} SL |
| 34 | + * @param {number} left |
| 35 | + */ |
| 36 | + static INSLEFT(_array, _current_item, SL, left, comp) { |
| 37 | + let j = SL; |
| 38 | + do { |
| 39 | + _array[j + 1] = _array[j]; |
| 40 | + j = j - 1; |
| 41 | + } |
| 42 | + while (comp.greaterThanOrEqual(j,left) && comp.lessThan(_current_item, _array[j])); |
| 43 | + _array[j + 1] = _current_item; |
| 44 | + } // end INSLEFT |
| 45 | + /** |
| 46 | + * @param {Object[]} _array |
| 47 | + * @param {number} SL |
| 48 | + * @param {number} SR |
| 49 | + */ |
| 50 | + static ISEQUAL(_array, SL, SR, comp) { |
| 51 | + for (let k = SL + 1; k <= SR - 1; k++) { |
| 52 | + if (comp.compare(_array[k],_array[SL] !== 0)) { |
| 53 | + this.SWAP(_array, k, SL); |
| 54 | + return k; |
| 55 | + } // end if |
| 56 | + } // end for |
| 57 | + return -1; |
| 58 | + } // end ISEQUAL |
| 59 | + /** |
| 60 | + * @param {Object[]} _array array to be sorted |
| 61 | + * @param {number} left index of left sorted array |
| 62 | + * @param {number} right index of right sorted array |
| 63 | + */ |
| 64 | + static Sort(_array = [], left = null, right = null) { |
| 65 | + let SL = left; |
| 66 | + let SR = right; |
| 67 | + let i; |
| 68 | + let comp = new Comparator(function(a,b){ |
| 69 | + let x = a; |
| 70 | + let y = b; |
| 71 | + if(typeof a === "string"){ |
| 72 | + x = a.length; |
| 73 | + } |
| 74 | + if(typeof b === "string"){ |
| 75 | + y = b.length; |
| 76 | + } |
| 77 | + if (x === y) { |
| 78 | + return 0; |
| 79 | + } |
| 80 | + return x < y ? -1 : 1; |
| 81 | + }) |
| 82 | + |
| 83 | + do { |
| 84 | + let calc = Math.round(SL + (SR - SL) / 2); |
| 85 | + this.SWAP(_array, SR, calc); |
| 86 | + if (comp.equal(_array[SL],_array[SR])) { |
| 87 | + if (this.ISEQUAL(_array, SL, SR, comp) === -1) { |
| 88 | + return; |
| 89 | + } // end if |
| 90 | + } // end if |
| 91 | + if (comp.greaterThan(_array[SL],_array[SR])) { |
| 92 | + this.SWAP(_array, SL, SR); |
| 93 | + } // end if |
| 94 | + if (SR - SL >= 100) { |
| 95 | + for (i = (SL + 1); i <= parseInt(Math.pow(SR - SL, 0.5)); i++) { |
| 96 | + if (comp.lessThan(_array[SR], _array[i])) { |
| 97 | + this.SWAP(_array, SR, i); |
| 98 | + } |
| 99 | + else if (comp.greaterThan(_array[SL], _array[i])) { |
| 100 | + this.SWAP(_array, SL, i); |
| 101 | + } // end if |
| 102 | + } // end for |
| 103 | + } |
| 104 | + else { |
| 105 | + i = SL + 1; |
| 106 | + } // end if |
| 107 | + let LC = _array[SL]; |
| 108 | + let RC = _array[SR]; |
| 109 | + do { |
| 110 | + let _current_item = _array[i]; |
| 111 | + if (comp.greaterThan(_current_item,RC)) { |
| 112 | + _array[i] = _array[SR - 1]; |
| 113 | + this.INSRIGHT(_array, _current_item, SR, right, comp); |
| 114 | + SR--; |
| 115 | + } |
| 116 | + else if (comp.lessThan(_current_item, LC)) { |
| 117 | + _array[i] = _array[SL + 1]; |
| 118 | + this.INSLEFT(_array, _current_item, SL, left, comp); |
| 119 | + SL++; i++; |
| 120 | + } |
| 121 | + else { |
| 122 | + i++; |
| 123 | + } // end if |
| 124 | + } |
| 125 | + while (i < SR) |
| 126 | + SL++; |
| 127 | + SR--; |
| 128 | + } |
| 129 | + while (SL < SR) |
| 130 | + } // end Sort |
| 131 | + sort(array) { |
| 132 | + if(array.length === 0) return []; |
| 133 | + BCIS.Sort(array, 0, array.length - 1); |
| 134 | + return array; |
| 135 | + } // end sort |
| 136 | +} |
0 commit comments