Merge pull request #10 from albertstill/improve-bubble-sort

stop bubble sort revisiting already sorted elements
This commit is contained in:
Oleksii Trekhleb 2018-05-24 09:22:08 +03:00 committed by GitHub
commit 99bef391d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 4 deletions

View File

@ -7,13 +7,13 @@ export default class BubbleSort extends Sort {
// Clone original array to prevent its modification. // Clone original array to prevent its modification.
const array = [...originalArray]; const array = [...originalArray];
for (let i = 0; i < array.length; i += 1) { for (let i = 1; i < array.length; i += 1) {
swapped = false; swapped = false;
// Call visiting callback. // Call visiting callback.
this.callbacks.visitingCallback(array[i]); 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. // Call visiting callback.
this.callbacks.visitingCallback(array[j]); this.callbacks.visitingCallback(array[j]);

View File

@ -9,8 +9,8 @@ import {
// Complexity constants. // Complexity constants.
const SORTED_ARRAY_VISITING_COUNT = 20; const SORTED_ARRAY_VISITING_COUNT = 20;
const NOT_SORTED_ARRAY_VISITING_COUNT = 280; const NOT_SORTED_ARRAY_VISITING_COUNT = 189;
const REVERSE_SORTED_ARRAY_VISITING_COUNT = 400; const REVERSE_SORTED_ARRAY_VISITING_COUNT = 209;
const EQUAL_ARRAY_VISITING_COUNT = 20; const EQUAL_ARRAY_VISITING_COUNT = 20;
describe('BubbleSort', () => { describe('BubbleSort', () => {