Repository navigation
Expand file tree
/
Copy pathbubbleSort.java
More file actions
92 lines (90 loc) · 4.94 KB
/
Copy pathbubbleSort.java
File metadata and controls
92 lines (90 loc) · 4.94 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.*; // 导入java.util包,包含Scanner等工具类
public class bubbleSort {
public static void main(String[] args) {
System.out.print("chose the type:\n1.double\n2.char\n3.string\nyou chose: "); // 提示用户选择排序对象类型
// 提示用户输入排序对象类型
Scanner input = new Scanner(System.in); // 创建Scanner对象用于接收用户输入
int type = input.nextInt(); // 读取用户输入的排序对象类型
switch (type) {
case 1:bubbleSortDouble(); // 调用排序方法对double型数组进行排序
break;
case 2:
bubbleSortChar(); // 调用排序方法对char型数组进行排序
break;
case 3:
bubbleSortString(); // 调用排序方法对String型数组进行排序
break;
default:
break;
}
input.close(); // 关闭Scanner对象释放资源
}
public static void bubbleSortDouble() {
System.out.println("Please input the length of the array:");
Scanner input = new Scanner(System.in); // 创建Scanner对象用于接收用户输入
int length = input.nextInt(); // 读取用户输入的数组长度
Double[] arry = new Double[length]; // 创建一个指定长度的双精度浮点数组
System.out.println("Please input the elements of the array:");
for (int i = 0; i < length; i++) {
arry[i] = input.nextDouble(); // 读取用户输入的数组元素
}
long startTime = System.currentTimeMillis(); // 记录开始时间
BUBBLESORT(arry); // 调用选择排序方法对数组进行排序
long endTime = System.currentTimeMillis(); // 记录结束时间
System.out.println("The sorted array is:");
for (Double d : arry) {
System.out.print(d + "\t"); // 输出排序后的数组元素
}
System.out.println("\nThe time of sorting the array is: " + (endTime - startTime) + "ms"); // 输出排序所用时间
input.close(); // 关闭Scanner对象释放资源
}
public static void bubbleSortChar() {
System.out.println("Please input the length of the array:");
Scanner input = new Scanner(System.in); // 创建Scanner对象用于接收用户输入
int length = input.nextInt(); // 读取用户输入的数组长度
Character[] arry = new Character[length]; // 创建一个指定长度的双精度浮点数组
System.out.println("Please input the elements of the array:");
for (int i = 0; i < length; i++) {
arry[i] = input.next().charAt(0); // 读取用户输入的数组元素
}
long startTime = System.currentTimeMillis(); // 记录开始时间
BUBBLESORT(arry); // 调用选择排序方法对数组进行排序
long endTime = System.currentTimeMillis(); // 记录结束时间
System.out.println("The sorted array is:");
for (Character d : arry) {
System.out.print(d + "\t"); // 输出排序后的数组元素
}
System.out.println("\nThe time of sorting the array is: " + (endTime - startTime) + "ms"); // 输出排序所用时间
input.close(); // 关闭Scanner对象释放资源
}
public static void bubbleSortString() {
System.out.println("Please input the length of the array:");
Scanner input = new Scanner(System.in); // 创建Scanner对象用于接收用户输入
int length = input.nextInt(); // 读取用户输入的数组长度
String[] arry = new String[length]; // 创建一个指定长度的双精度浮点数组
System.out.println("Please input the elements of the array:");
for (int i = 0; i < length; i++) {
arry[i] = input.nextLine(); // 读取用户输入的数组元素
}
long startTime = System.currentTimeMillis(); // 记录开始时间
BUBBLESORT(arry); // 调用选择排序方法对数组进行排序
long endTime = System.currentTimeMillis(); // 记录结束时间
System.out.println("The sorted array is:");
for (String d : arry) {
System.out.print(d + "\t"); // 输出排序后的数组元素
}
System.out.println("\nThe time of sorting the array is: " + (endTime - startTime) + "ms"); // 输出排序所用时间
input.close(); // 关闭Scanner对象释放资源
}
public static <T extends Comparable<T>> void BUBBLESORT(T[] arry) {
for (int i = 0; i < arry.length-1; i++) {
for (int j = 0; j < arry.length-1-i; j++) {
if (arry[j].compareTo(arry[j+1])==1) { // 判断是否需要交换
T temp = arry[j]; // 交换元素
arry[j] = arry[j+1];
arry[j+1] = temp;
}
}
}
}
}