Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit99bef39

Browse files
authored
Merge pull requesttrekhleb#10 from albertstill/improve-bubble-sort
stop bubble sort revisiting already sorted elements
2 parents35fff1f +f34fd84 commit99bef39

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

‎src/algorithms/sorting/bubble-sort/BubbleSort.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ export default class BubbleSort extends Sort {
77
// Clone original array to prevent its modification.
88
constarray=[...originalArray];
99

10-
for(leti=0;i<array.length;i+=1){
10+
for(leti=1;i<array.length;i+=1){
1111
swapped=false;
1212

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

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

‎src/algorithms/sorting/bubble-sort/__test__/BubbleSort.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99

1010
// Complexity constants.
1111
constSORTED_ARRAY_VISITING_COUNT=20;
12-
constNOT_SORTED_ARRAY_VISITING_COUNT=280;
13-
constREVERSE_SORTED_ARRAY_VISITING_COUNT=400;
12+
constNOT_SORTED_ARRAY_VISITING_COUNT=189;
13+
constREVERSE_SORTED_ARRAY_VISITING_COUNT=209;
1414
constEQUAL_ARRAY_VISITING_COUNT=20;
1515

1616
describe('BubbleSort',()=>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp