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
adding the test cases
SNavleen committed Oct 29, 2018
commit 923804a6de3dd500b122a658b5d03e370b321912
2,850 changes: 1,425 additions & 1,425 deletions package-lock.json

Large diffs are not rendered by default.

29 changes: 16 additions & 13 deletions src/algorithms/sorting/odd-even-sort/OddEvenSort.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
import Sort from '../Sort';

export default class OddEvenSort extends Sort {
swap(array, i, j){
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}

sort(originalArray) {
// Clone original array to prevent its modification.
const array = [...originalArray];
const sorted = false;
let sorted = false;

// Go through all array elements...
while (!sorted) {
sorted = true;
for(let i = 1; i < array.length; i+2){
// Sort all the odd indexes
for (let i = 1; i < array.length - 1; i += 2) {
// Call visiting callback.
this.callbacks.visitingCallback(array[i]);
if(array[i] > array[i+1]){
swap(array, i, i+1);

// Swap elements if they are in wrong order.
if (this.comparator.lessThan(array[i + 1], array[i])) {
// Swap the values
[array[i], array[i + 1]] = [array[i + 1], array[i]];
sorted = false;
}
}
for(let i = 0; i < array.length - 1; i+2){

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

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

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

describe('OddEvenSort', () => {
it('should sort array', () => {
SortTester.testSort(OddEvenSort);
});

it('should sort array with custom comparator', () => {
SortTester.testSortWithCustomComparator(OddEvenSort);
});

it('should do stable sorting', () => {
SortTester.testSortStability(OddEvenSort);
});

it('should sort negative numbers', () => {
SortTester.testNegativeNumbersSort(OddEvenSort);
});

// 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 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(
OddEvenSort,
reverseArr,
REVERSE_SORTED_ARRAY_VISITING_COUNT,
);
});
});