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
Working on getting tests to 100%
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 87b3bcf673d29760162fccb1fb45f7e4a1725585
8 changes: 8 additions & 0 deletions src/algorithms/sorting/SortTester.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@ export const sortedArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
export const reverseArr = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
export const notSortedArr = [15, 8, 5, 12, 10, 1, 16, 9, 11, 7, 20, 3, 2, 6, 17, 18, 4, 13, 14, 19];
export const equalArr = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
export const largeArr = [5, 53, 38, 93, 200, 140, 210, 73, 178, 231, 145, 91, 240, 250, 108, 121, 238, 55, 233, 46, 136, 74, 40, 202, 86, 43, 42, 218, 193, 141, 232, 95, 236, 75, 128, 209, 180, 80, 88, 104, 111, 216, 13, 179, 191, 157, 126, 14, 150, 90, 134, 27, 142, 11, 33, 123, 51, 139, 20, 49, 198, 138, 65, 82, 22, 243, 241, 21, 135, 132, 79, 107, 154, 12, 189, 100, 203, 59, 89, 4, 130, 133, 223, 6, 190, 162, 156, 118, 148, 67, 163, 181, 225, 153, 244, 47, 129, 57, 92, 177, 185, 68, 81, 64, 17, 246, 39, 201, 161, 2, 30, 26, 41, 45, 101, 114, 222, 35, 164, 195, 78, 146, 155, 173, 98, 23, 215, 213, 37, 187, 60, 247, 149, 212, 15, 63, 70, 32, 171, 248, 62, 221, 94, 109, 224, 28, 226, 9, 61, 184, 152, 217, 34, 115, 31, 56, 158, 36, 228, 208, 245, 183, 77, 72, 160, 25, 205, 169, 96, 113, 102, 10, 122, 199, 69, 220, 76, 125, 175, 24, 188, 159, 168, 7, 182, 234, 50, 174, 112, 229, 151, 99, 197, 85, 186, 230, 127, 227, 1, 167, 124, 48, 206, 18, 116, 166, 131, 137, 117, 110, 66, 192, 165, 83, 219, 19, 84, 176, 144, 172, 211, 119, 214, 103, 235, 196, 204, 97, 8, 87, 44, 249, 170, 194, 120, 237, 105, 147, 16, 242, 71, 58, 106, 3, 239, 207, 52, 29, 54, 143];
export const mostlySortedlargeArr = [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, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 237, 105, 147, 16, 242, 71, 58, 106, 3, 239, 207, 52, 29, 54, 143];

export class SortTester {
static testSort(SortingClass) {
@@ -62,4 +64,10 @@ export class SortTester {

expect(visitingCallback).toHaveBeenCalledTimes(numberOfVisits);
}
static testAlgorithmTimeWithoutArray(SortingClass, arrayToBeSorted) {
const visitingCallback = jest.fn();
const callbacks = { visitingCallback };
const sorter = new SortingClass(callbacks);
expect(()=>sorter.sort(arrayToBeSorted)).toThrow();
}
}
Original file line number Diff line number Diff line change
@@ -146,7 +146,7 @@ export default class BCIS extends Sort {
} // end Sort
sort(array) {
if (Array.isArray(array) === false) {
throw exception('An Array object is required.');
throw new Error('An Array object is required.');
}
const len = array.length;
if (len <= 1) return array;
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ import {
notSortedArr,
reverseArr,
sortedArr,
largeArr,
mostlySortedlargeArr,
SortTester,
} from '../../SortTester';

@@ -12,6 +14,8 @@ const SORTED_ARRAY_VISITING_COUNT = 26;
const NOT_SORTED_ARRAY_VISITING_COUNT = 20;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 26;
const EQUAL_ARRAY_VISITING_COUNT = 90;
const LARGE_ARRAY_VISITING_COUNT = 705;
const MOSTLY_LARGE_ARRAY_VISITING_COUNT = 293;

describe('BCIS', () => {
it('should sort array', () => {
@@ -52,4 +56,25 @@ describe('BCIS', () => {
REVERSE_SORTED_ARRAY_VISITING_COUNT,
);
});
it('should visit NOT SORTED LARGE array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BCIS,
largeArr,
LARGE_ARRAY_VISITING_COUNT,
);
});
it('should visit MOSTLY SORTED LARGE array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
BCIS,
mostlySortedlargeArr,
MOSTLY_LARGE_ARRAY_VISITING_COUNT,
);
});
it('An Array object is required', () => {
SortTester.testAlgorithmTimeWithoutArray(
BCIS,
null,
0,
);
});
});