Skip to content

Commit

Permalink
Merge pull request trekhleb#10 from albertstill/improve-bubble-sort
Browse files Browse the repository at this point in the history
stop bubble sort revisiting already sorted elements
  • Loading branch information
Muhammad Usman committed May 24, 2018
2 parents 6a60e4a + 538b711 commit 3cdaee3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/algorithms/sorting/bubble-sort/BubbleSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export default class BubbleSort extends Sort {
// Clone original array to prevent its modification.
const array = [...originalArray];

for (let i = 0; i < array.length; i += 1) {
for (let i = 1; i < array.length; i += 1) {
swapped = false;

// Call visiting callback.
this.callbacks.visitingCallback(array[i]);

for (let j = 0; j < array.length - 1; j += 1) {
for (let j = 0; j < array.length - i; j += 1) {
// Call visiting callback.
this.callbacks.visitingCallback(array[j]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {

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

describe('BubbleSort', () => {
Expand Down

0 comments on commit 3cdaee3

Please sign in to comment.