-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.java
More file actions
61 lines (53 loc) · 1.23 KB
/
Copy pathBubbleSort.java
File metadata and controls
61 lines (53 loc) · 1.23 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
61
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
/*
*12 12 32 65 98
78 69 36 25 14
78 65 25 85 10
89 56 23 12 45
73 91 64 82 55
* */
public class BubbleSort {
public void sortArray(){
Scanner sc = new Scanner(System.in);
System.out.print("请输入要排序的数据的个数,并输入这些数据:");
int n = sc.nextInt();
int[] num = new int[n];
for(int i = 0; i < n; i++){
num[i] = sc.nextInt();
}
System.out.println("排序前的数据:");
for(int i = 0; i < n; i++){
System.out.print(num[i] + " ");
}
System.out.println();
/*for(int i = 0; i < n - 1; i++){
for(int j = i + 1; j < n; j++){
if(num[i] > num[j]){
num[i] = num[i] ^ num[j];
num[j] = num[i] ^ num[j];
num[i] = num[i] ^ num[j];
}
}
}*/
for(int i = 0; i < n - 1; i++){
for(int j = 0; j < n - 1 - i; j++){
if(num[j] > num[j + 1]){
num[j] = num[j] ^ num[j + 1];
num[j + 1] = num[j] ^ num[j + 1];
num[j] = num[j] ^ num[j + 1];
}
}
}
System.out.println("排序后的数据:");
for(int i = 0; i < n; i++){
System.out.print(num[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
BubbleSort bs = new BubbleSort();
bs.sortArray();
}
}