Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: trekhleb/javascript-algorithms
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: edumoreira1506/javascript-algorithms
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: gnome-sort
Choose a head ref
Able to merge. These branches can be automatically merged.
  • 5 commits
  • 3 files changed
  • 1 contributor

Commits on Sep 10, 2019

  1. Copy the full SHA
    5b7fbc1 View commit details
  2. Copy the full SHA
    abdf541 View commit details
  3. README.md introducted

    edumoreira1506 committed Sep 10, 2019
    Copy the full SHA
    3b3d7b9 View commit details
  4. Copy the full SHA
    e1b3871 View commit details
  5. Fix gnome algorithm

    edumoreira1506 committed Sep 10, 2019
    Copy the full SHA
    5d9f19b View commit details
38 changes: 38 additions & 0 deletions src/algorithms/sorting/gnome-sort/GnomeSort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Sort from '../Sort';

export default class GnomeSort extends Sort {
/**
* @param {number[]} originalArray
*/
sort(originalArray) {
const sortedArray = originalArray.map(element => element);

// Variable that will be itering during loop
let index = 0;

if (sortedArray.length <= 1) {
return sortedArray;
}

// Loop for sort the array
while (index < sortedArray.length) {
// Detects the first iteration
if (index === 0) {
index += 1;
}

// Detects if the current position is smaller of previous
if (sortedArray[index] >= sortedArray[index - 1]) {
index += 1;
} else {
// Change the position of current position for before
const temp = sortedArray[index];
sortedArray[index] = sortedArray[index - 1];
sortedArray[index - 1] = temp;
index -= 1;
}
}

return sortedArray;
}
}
25 changes: 25 additions & 0 deletions src/algorithms/sorting/gnome-sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Gnome sort

Gnome sort (dubbed stupid sort) is a sorting algorithm originally
proposed by an Iranian computer scientist Hamid Sarbazi-Azad
(professor of Computer Engineering at Sharif University of Technology)
in 2000. The sort was first called stupid sort (not to be confused
with bogosort), and then later described by Dick Grune and named gnome sort.

The gnome sort is a sorting algorithm which is similar to insertion
sort in that it works with one item at a time but gets the item to
the proper place by a series of swaps, similar to a bubble sort.
It is conceptually simple, requiring no nested loops. The average
running time is O(n2) but tends towards O(n) if the list is initially
almost sorted.

The algorithm finds the first place where two adjacent elements are
in the wrong order and swaps them. It takes advantage of the fact that
performing a swap can introduce a new out-of-order adjacent pair
next to the previously swapped elements. It does not assume that elements
forward of the current position are sorted, so it only needs to check
the position directly previous to the swapped elements.

## References

- [Wikipedia](https://en.wikipedia.org/wiki/Gnome_sort)
14 changes: 14 additions & 0 deletions src/algorithms/sorting/gnome-sort/__test__/GnomeSort.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import GnomeSort from '../GnomeSort';
import {
SortTester,
} from '../../SortTester';

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

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