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

A new sorting algorithm implementation known as Odd-Even #241

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added in all the test cases and fixed the visiting count since it sta…
…tes 'each time the sorting function is visiting the next element.'
SNavleen committed Oct 29, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit c4aaf5d251ee54a84e37ca5cc13a018a1f26a391
5 changes: 3 additions & 2 deletions src/algorithms/sorting/odd-even-sort/OddEvenSort.js
Original file line number Diff line number Diff line change
@@ -9,10 +9,11 @@ export default class OddEvenSort extends Sort {
// Go through all array elements...
while (!sorted) {
sorted = true;

// Sort all the odd indexes
for (let i = 1; i < array.length - 1; i += 2) {
// Call visiting callback.
this.callbacks.visitingCallback(array[i]);
this.callbacks.visitingCallback(array[i + 1]);

// Swap elements if they are in wrong order.
if (this.comparator.lessThan(array[i + 1], array[i])) {
@@ -25,7 +26,7 @@ export default class OddEvenSort extends Sort {
// Sort all the even indexes
for (let i = 0; i < array.length - 1; i += 2) {
// Call visiting callback.
this.callbacks.visitingCallback(array[i]);
this.callbacks.visitingCallback(array[i + 1]);

// Swap elements if they are in wrong order.
if (this.comparator.lessThan(array[i + 1], array[i])) {
54 changes: 27 additions & 27 deletions src/algorithms/sorting/odd-even-sort/__test__/OddEvenSort.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import OddEvenSort from '../OddEvenSort';
import {
// equalArr,
// notSortedArr,
equalArr,
notSortedArr,
reverseArr,
// sortedArr,
sortedArr,
SortTester,
} from '../../SortTester';

// Complexity constants.
// const SORTED_ARRAY_VISITING_COUNT = 20;
// const NOT_SORTED_ARRAY_VISITING_COUNT = 189;
const EQUAL_ARRAY_VISITING_COUNT = 19;
const SORTED_ARRAY_VISITING_COUNT = 19;
const NOT_SORTED_ARRAY_VISITING_COUNT = 171;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209;
// const EQUAL_ARRAY_VISITING_COUNT = 20;

describe('OddEvenSort', () => {
it('should sort array', () => {
@@ -30,29 +30,29 @@ describe('OddEvenSort', () => {
SortTester.testNegativeNumbersSort(OddEvenSort);
});

// it('should visit EQUAL array element specified number of times', () => {
// SortTester.testAlgorithmTimeComplexity(
// OddEvenSort,
// equalArr,
// EQUAL_ARRAY_VISITING_COUNT,
// );
// });
it('should visit EQUAL array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
OddEvenSort,
equalArr,
EQUAL_ARRAY_VISITING_COUNT,
);
});

// it('should visit SORTED array element specified number of times', () => {
// SortTester.testAlgorithmTimeComplexity(
// OddEvenSort,
// sortedArr,
// SORTED_ARRAY_VISITING_COUNT,
// );
// });
it('should visit SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
OddEvenSort,
sortedArr,
SORTED_ARRAY_VISITING_COUNT,
);
});

// it('should visit NOT SORTED array element specified number of times', () => {
// SortTester.testAlgorithmTimeComplexity(
// OddEvenSort,
// notSortedArr,
// NOT_SORTED_ARRAY_VISITING_COUNT,
// );
// });
it('should visit NOT SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(
OddEvenSort,
notSortedArr,
NOT_SORTED_ARRAY_VISITING_COUNT,
);
});

it('should visit REVERSE SORTED array element specified number of times', () => {
SortTester.testAlgorithmTimeComplexity(