forked from shijiebei2009/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBubbleSort.java
More file actions
46 lines (40 loc) · 963 Bytes
/
Copy pathBubbleSort.java
File metadata and controls
46 lines (40 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package cn.codepub.algorithms.sorting;
import org.junit.Test;
import java.util.Arrays;
/**
* <p>
* Created with IntelliJ IDEA. 2016/1/8 19:37
* </p>
* <p>
* ClassName:BubbleSort
* </p>
* <p>
* Description:冒泡排序
* </P>
*
* @author Wang Xu
* @version V1.0.0
* @since V1.0.0
*/
public class BubbleSort {
public void bubbleSort(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = nums.length - 1; j > i; j--) {
if (nums[i] > nums[j]) {
swap(nums, i, j);
}
}
}
}
public void swap(int[] nums, int one, int two) {
int temp = nums[one];
nums[one] = nums[two];
nums[two] = temp;
}
@Test
public void test() {
int[] nums = new int[]{7, 3, 4, 23, 3, 9};
bubbleSort(nums);
System.out.println(Arrays.toString(nums));
}
}