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 all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -111,6 +111,7 @@ a set of rules that precisely define a sequence of operations.
* `B` [Shellsort](src/algorithms/sorting/shell-sort)
* `B` [Counting Sort](src/algorithms/sorting/counting-sort)
* `B` [Radix Sort](src/algorithms/sorting/radix-sort)
* `B` [Odd-Even Sort](src/algorithms/sorting/odd-even-sort)
* **Linked Lists**
* `B` [Straight Traversal](src/algorithms/linked-list/traversal)
* `B` [Reverse Traversal](src/algorithms/linked-list/reverse-traversal)
9,601 changes: 0 additions & 9,601 deletions package-lock.json

This file was deleted.

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

export default class OddEvenSort extends Sort {
sort(originalArray) {
// Clone original array to prevent its modification.
const array = [...originalArray];
let sorted = false;

// 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 + 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;
}
}

// Sort all the even indexes
for (let i = 0; i < array.length - 1; i += 2) {
// Call visiting callback.
this.callbacks.visitingCallback(array[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;
}
}
}
return array;
}
}
17 changes: 17 additions & 0 deletions src/algorithms/sorting/odd-even-sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Odd-Even Sort

Odd–Even sort (also known as brick sort) is a relatively simple sorting algorithm. Developed originally for use on parallel processors with local interconnections. It is a comparison sort related to bubble sort, with which it shares many characteristics. It functions by comparing all odd/even indexed pairs of adjacent elements in the list and, if a pair is in the wrong order (the first is larger than the second) the elements are switched. The next step repeats this for even/odd indexed pairs (of adjacent elements). Then it alternates between odd/even and even/odd steps until the list is sorted.


![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/1/1b/Odd_even_sort_animation.gif)

## Complexity

| Name | Best | Average | Worst | Memory | Stable | Comments |
| ----------------------- | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :---------------------------------------- |
| **Odd-Even sort** | n | n<sup>2</sup> | n<sup>2</sup> | 1 | Yes | Can be run on parallel processors easily. |

## References

- [Wikipedia](https://en.wikipedia.org/wiki/Odd–even_sort)
<!-- - [YouTube](https://www.youtube.com/watch?v=6Gv8vg0kcHc&index=27&t=0s&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8) -->
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 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;

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,
);
});
});