-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.java
More file actions
45 lines (40 loc) · 1.17 KB
/
Sort.java
File metadata and controls
45 lines (40 loc) · 1.17 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
public class Sort {
public static void main(String args[]) {
int arr[] = { 1, 5, 6, 3, 2, 4 };
Sort sort = new Sort();
int ar1[] = sort.bubbleSort(arr, "ESC");
for (int num : ar1) {
System.out.println(num + "");
}
}
/**
* 冒泡排序
* @param arr 整数类型数组
* @param sortType 排序类型 顺序传 ESC 倒序 DESC
* @return
*/
public int[] bubbleSort(int arr[], String sortType) {
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
if (sortType == "ESC") {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
} else {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
return arr;
}
public int [] insertSort(int [] arr,String sortType){
return arr;
}
}