-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSorting.java
More file actions
60 lines (49 loc) · 1.35 KB
/
BubbleSorting.java
File metadata and controls
60 lines (49 loc) · 1.35 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package Algithorm;
/**
* Created by caixin on 21/11/17.
*/
public class BubbleSorting {
/**
* 从小到大 排序
*
*/
public static void bubbleSort2(int[] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if (arr[j]>=arr[j+1]){
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
/**
* sorting array
*
* order
* 由大到小 排序
*
* @param arr
*/
public void BubbleSort(int[] arr){
int i,j,temp;
for (i = 0; i < arr.length -1; i++) {
for (j = i +1; j <arr.length ; j++) { // 遍历数组, 到最后一个位置
if (arr[i] < arr[j]){ //arr[i] 和 之后的所有的元素比较, 把小于的数 放到后面位置
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
BubbleSorting bubbleSorting = new BubbleSorting();
int[] myArr= {2,6,4,8,9};
bubbleSorting.BubbleSort(myArr);
for (int i = 0; i < myArr.length; i++) {
System.out.println(myArr[i]);
}
}
}