Skip to content

Commit f1a7626

Browse files
author
Christian Matsoukis
committedJun 13, 2018
added Bidirectional Conditional Insertion Sort
1 parent 9de6bc7 commit f1a7626

File tree

5 files changed

+1586
-1386
lines changed

5 files changed

+1586
-1386
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ a set of rules that precisely define a sequence of operations.
8383
* [Shellsort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/shell-sort)
8484
* [Counting Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/counting-sort)
8585
* [Radix Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/radix-sort)
86+
* [Bidirectional Conditional Insertion Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/bidirectional-conditional-insertion-sort)
8687
* **Trees**
8788
* [Depth-First Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/depth-first-search) (DFS)
8889
* [Breadth-First Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/breadth-first-search) (BFS)

‎package-lock.json

+1,386-1,386
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Bidirectional Conditional Insertion Sort
2+
3+
This is a javascript implementation of the Bidirectional Conditional Insertion Sort algorithm published by Future Computer Systems Volume 71, June 2017, Pages 102-112. Read about it here:
4+
5+
[Journal](https://www.sciencedirect.com/science/article/pii/S0167739X17301711?via%3Dihub)
6+
7+
## References
8+
- [Science Direct](https://www.sciencedirect.com/science/article/pii/S0167739X17301711?via%3Dihub)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import BCIS from '../BCIS';
2+
import {
3+
equalArr,
4+
notSortedArr,
5+
reverseArr,
6+
sortedArr,
7+
SortTester,
8+
} from '../../SortTester';
9+
10+
// Complexity constants.
11+
const SORTED_ARRAY_VISITING_COUNT = 0;
12+
const NOT_SORTED_ARRAY_VISITING_COUNT = 0;
13+
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 0;
14+
const EQUAL_ARRAY_VISITING_COUNT = 0;
15+
16+
describe('BCIS', () => {
17+
it('should sort array', () => {
18+
SortTester.testSort(BCIS);
19+
});
20+
21+
it('should sort array with custom comparator', () => {
22+
SortTester.testSortWithCustomComparator(BCIS);
23+
});
24+
it('should visit EQUAL array element specified number of times', () => {
25+
SortTester.testAlgorithmTimeComplexity(
26+
BCIS,
27+
equalArr,
28+
EQUAL_ARRAY_VISITING_COUNT,
29+
);
30+
});
31+
32+
it('should visit SORTED array element specified number of times', () => {
33+
SortTester.testAlgorithmTimeComplexity(
34+
BCIS,
35+
sortedArr,
36+
SORTED_ARRAY_VISITING_COUNT,
37+
);
38+
});
39+
40+
it('should visit NOT SORTED array element specified number of times', () => {
41+
SortTester.testAlgorithmTimeComplexity(
42+
BCIS,
43+
notSortedArr,
44+
NOT_SORTED_ARRAY_VISITING_COUNT,
45+
);
46+
});
47+
48+
it('should visit REVERSE SORTED array element specified number of times', () => {
49+
SortTester.testAlgorithmTimeComplexity(
50+
BCIS,
51+
reverseArr,
52+
REVERSE_SORTED_ARRAY_VISITING_COUNT,
53+
);
54+
});
55+
});

0 commit comments

Comments
 (0)
Please sign in to comment.