-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort1.java
More file actions
75 lines (60 loc) · 1.7 KB
/
Copy pathSort1.java
File metadata and controls
75 lines (60 loc) · 1.7 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
import java.util.*;
/**
* @author coderjoin
* @date 2019-11-13 16:37
*/
public class Sort1 {
public static void main(String[] args) {
int[] a = {1,2,6,4,5};
Sort1 s = new Sort1();
//s.sortBubble(a);
s.selectSort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
public void sortBubble(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
int tmp = array[j + 1];
array[j + 1] = array[j];
array[j] = tmp;
}
}
}
}
public void selectSort(int[] array) {
for (int i = 0; i < array.length -1; i++) {
int m = i;
for (int j = i; j < array.length; j++) {
if (array[m] < array[j]) {
m = j;
}
if (array[m] != array[j]) {
swap(array,m,j);
}
}
}
}
public void swap(int[] array, int m, int j) {
int tmp = array[m];
array[m] = array[j];
array[j] = tmp;
}
public ArrayList<Integer> test(int[] array1, int[] array2) {
Set<Integer> set = new TreeSet<>();
for (int i = 0; i < array1.length; i++) {
set.add(array1[i]);
}
for (int i = 0; i < array2.length; i++) {
set.add(array2[i]);
}
ArrayList<Integer> list = new ArrayList<>();
for (Integer i : set) {
list.add(i);
}
Collections.sort(list);
return list;
}
}