Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bidirectional conditional insertion sort #68

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Passing eslist && npm test.
Christian Matsoukis committed Jun 14, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit bfce00a30eb34c70291cfcfbf38c44ab5cab3104
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
import Sort from '../Sort';
import Comparator from '../../../utils/comparator/Comparator';

function exception(message) {
return {
Error: true,
Message: message,
};
}
export default class BCIS extends Sort {
/**
* @param {Object[]} _array array to be swapped
* @param {number} a index a to be swapped with index b
* @param {number} b index b to be swapped with index a
*/
static SWAP(_array = [], i, j) {
const temp = _array[i];
_array[i] = _array[j];
_array[j] = temp;
SWAP(_array = [], i, j) {
const arr = [..._array];
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
} // end SWAP
/**
* @param {Object[]} _array
* @param {*} currentItem
* @param {number} SR
* @param {number} right
*/
static INSRIGHT(_array, currentItem, SR, right, comp) {
INSRIGHT(_array, currentItem, SR, right, comp) {
const arr = [..._array];
let j = SR;
do {
_array[j - 1] = _array[j];
arr[j - 1] = arr[j];
j += 1;
}
while (comp.lessThanOrEqual(j, right) && comp.greaterThan(currentItem, _array[j]));
_array[j - 1] = currentItem;
while (comp.lessThanOrEqual(j, right) && comp.greaterThan(currentItem, arr[j]));
arr[j - 1] = currentItem;
return arr;
} // end INSRIGHT
/**
*
@@ -34,25 +44,28 @@ export default class BCIS extends Sort {
* @param {number} SL
* @param {number} left
*/
static INSLEFT(_array, currentItem, SL, left, comp) {
INSLEFT(_array, currentItem, SL, left, comp) {
const arr = [..._array];
let j = SL;
do {
_array[j + 1] = _array[j];
arr[j + 1] = arr[j];
j -= 1;
}
while (comp.greaterThanOrEqual(j, left) && comp.lessThan(currentItem, _array[j]));
_array[j + 1] = currentItem;
while (comp.greaterThanOrEqual(j, left) && comp.lessThan(currentItem, arr[j]));
arr[j + 1] = currentItem;
return arr;
} // end INSLEFT
/**
* @param {Object[]} _array
* @param {number} SL
* @param {number} SR
*/
static ISEQUAL(_array, SL, SR, comp) {
ISEQUAL(_array, SL, SR, comp) {
let arr = [..._array];
for (let k = SL + 1; k <= SR - 1; k += 1) {
if (comp.compare(_array[k], _array[SL] !== 0)) {
this.SWAP(_array, k, SL);
return k;
if (comp.compare(arr[k], arr[SL] !== 0)) {
arr = this.SWAP(arr, k, SL);
return arr;
} // end if
} // end for
return -1;
@@ -62,7 +75,8 @@ export default class BCIS extends Sort {
* @param {number} left index of left sorted array
* @param {number} right index of right sorted array
*/
static Sort(_array = [], left = null, right = null) {
SORT(_array = [], left = null, right = null) {
let arr = [..._array];
let SL = left;
let SR = right;
let i;
@@ -83,37 +97,40 @@ export default class BCIS extends Sort {

do {
const calc = Math.round(SL + ((SR - SL) / 2));
this.SWAP(_array, SR, calc);
if (comp.equal(_array[SL], _array[SR])) {
if (this.ISEQUAL(_array, SL, SR, comp) === -1) {
return;
arr = this.SWAP(arr, SR, calc);
if (comp.equal(arr[SL], arr[SR])) {
const copy = [...arr];
arr = this.ISEQUAL(arr, SL, SR, comp);
if (arr === -1) {
arr = [...copy];
break;
} // end if
} // end if
if (comp.greaterThan(_array[SL], _array[SR])) {
this.SWAP(_array, SL, SR);
if (comp.greaterThan(arr[SL], arr[SR])) {
arr = this.SWAP(arr, SL, SR);
} // end if
if (SR - SL >= 100) {
for (i = (SL + 1); i <= parseInt((SR - SL) ** 0.5, 10); i += 1) {
if (comp.lessThan(_array[SR], _array[i])) {
this.SWAP(_array, SR, i);
} else if (comp.greaterThan(_array[SL], _array[i])) {
this.SWAP(_array, SL, i);
if (comp.lessThan(arr[SR], arr[i])) {
arr = this.SWAP(arr, SR, i);
} else if (comp.greaterThan(arr[SL], arr[i])) {
arr = this.SWAP(arr, SL, i);
} // end if
} // end for
} else {
i = SL + 1;
} // end if
const LC = _array[SL];
const RC = _array[SR];
const LC = arr[SL];
const RC = arr[SR];
do {
const currentItem = _array[i];
const currentItem = arr[i];
if (comp.greaterThan(currentItem, RC)) {
_array[i] = _array[SR - 1];
this.INSRIGHT(_array, currentItem, SR, right, comp);
arr[i] = arr[SR - 1];
arr = this.INSRIGHT(arr, currentItem, SR, right, comp);
SR -= 1;
} else if (comp.lessThan(currentItem, LC)) {
_array[i] = _array[SL + 1];
this.INSLEFT(_array, currentItem, SL, left, comp);
arr[i] = arr[SL + 1];
arr = this.INSLEFT(arr, currentItem, SL, left, comp);
SL += 1; i += 1;
} else {
i += 1;
@@ -124,10 +141,15 @@ export default class BCIS extends Sort {
SR -= 1;
}
while (SL < SR);
return arr;
} // end Sort
sort(array) {
if (array.length === 0) return [];
BCIS.Sort(array, 0, array.length - 1);
return array;
if (Array.isArray(array) === false) {
throw exception('An Array object is required.');
}
const len = array.length;
if (len <= 1) return array;
const returnArr = this.SORT(array, 0, array.length - 1);
return returnArr;
} // end sort
}