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
Next Next commit
first commit for odd even sorting
SNavleen committed Oct 28, 2018
commit 7a6cd8cd283e03478a30801bf69d0e9bd96c9ad9
37 changes: 37 additions & 0 deletions src/algorithms/sorting/odd-even-sort/OddEvenSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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;

// Go through all array elements...
while (!sorted) {
sorted = true;
for(let i = 1; i < array.length; i+2){
// Call visiting callback.
this.callbacks.visitingCallback(array[i]);
if(array[i] > array[i+1]){
swap(array, i, i+1);
sorted = false;
}
}
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);
sorted = false;
}
}
}
return array;
}
}
16 changes: 16 additions & 0 deletions src/algorithms/sorting/odd-even-sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Odd-Even Sort

Odd-Even sort

![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) -->
Empty file.