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 5f81fa2

Browse files
Add bubble sort code
1 parent 3bf8b02 commit 5f81fa2

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

‎src/test/java/sort/BubbleSort.java‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sort;
22

33
import org.junit.Test;
4+
import utils.Utils;
45

56
import static org.hamcrest.CoreMatchers.is;
67
import static org.junit.Assert.assertThat;
@@ -14,13 +15,7 @@ public class BubbleSort {
1415

1516
@Test
1617
public void test() {
17-
int[] arr = new int[5];
18-
arr[0] = 2;
19-
arr[1] = 1;
20-
arr[2] = 4;
21-
arr[3] = 0;
22-
arr[4] = 3;
23-
18+
int[] arr = {2,1,4,0,3};
2419
int[] sortedArr = new int[arr.length];
2520
for (int i = 0; i < sortedArr.length; i++) {
2621
sortedArr[i] = i;
@@ -32,9 +27,7 @@ public int[] sort(int[] arr) {
3227
for (int i = 0; i < arr.length - 1; i++) {
3328
for (int j = i + 1; j < arr.length; j++) {
3429
if (arr[i] > arr[j]) {
35-
int temp = arr[i];
36-
arr[i] = arr[j];
37-
arr[j] = temp;
30+
Utils.swapValue(arr, i, j);
3831
}
3932
}
4033
}

0 commit comments

Comments
(0)

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