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

Commit c18cd33

Browse files
committed
AbstractSortArray, SortArray: Moved shuffle() from abstract class to interface (Java SE 8+).
1 parent c5cc027 commit c18cd33

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

‎src/io/nayuki/sortalgodemo/core/AbstractSortArray.java‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,6 @@ public boolean compareAndSwap(int i, int j) {
8585
}
8686

8787

88-
public void shuffle() {
89-
// Durstenfeld shuffle algorithm
90-
for (int i = length() - 1; i > 0; i--)
91-
swap(i, random.nextInt(i + 1));
92-
}
93-
94-
9588
/* Sorting progress visualization */
9689

9790
// Does nothing by default; can be overridden.

‎src/io/nayuki/sortalgodemo/core/SortArray.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ public interface SortArray {
8686
/**
8787
* Randomizes the order of all elements in this array.
8888
*/
89-
public void shuffle();
89+
public default void shuffle() {
90+
// Durstenfeld shuffle algorithm
91+
for (int i = length() - 1; i > 0; i--)
92+
swap(i, random.nextInt(i + 1));
93+
}
9094

9195

9296
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /