Skip to content

Commit 5267ed5

Browse files
committed
第一次提交 for 简单排序 v1
0 parents  commit 5267ed5

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

algorithm.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.oslee.algorithm.sort;
2+
3+
4+
public class Bubble {
5+
6+
/*
7+
对数组内的元素进行排序
8+
*/
9+
public static void sort(Comparable[] a){
10+
for (int i = a.length - 1; i > 0; i--){
11+
for(int j = 0; j < i; j++){
12+
if (greater(a[j], a[j + 1])){
13+
exch(a, j, j + 1);
14+
}
15+
}
16+
}
17+
}
18+
19+
/*
20+
判断v是否大于w
21+
*/
22+
private static boolean greater(Comparable v, Comparable w){
23+
return v.compareTo(w) > 0;
24+
}
25+
26+
/*
27+
交换a数组中, 索引i和索引j处的值
28+
*/
29+
private static void exch(Comparable[] a, int i, int j){
30+
Comparable temp;
31+
temp = a[i];
32+
a[i] = a[j];
33+
a[j] = temp;
34+
}
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.oslee.algorithm.sort;
2+
3+
/**
4+
* @version:
5+
* @description: 定义一个学生类Student, 具有年龄age和姓名username两个属性, 并通过Comparable接口提供比较规则;
6+
* @author: Lee
7+
* @create: 2020-03-04 22:21
8+
*/
9+
public class Student implements Comparable<Student> {
10+
private String username;
11+
private int age;
12+
13+
public String getUsername() {
14+
return username;
15+
}
16+
17+
public void setUsername(String username) {
18+
this.username = username;
19+
}
20+
21+
public int getAge() {
22+
return age;
23+
}
24+
25+
public void setAge(int age) {
26+
this.age = age;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "Student{" +
32+
"username='" + username + '\'' +
33+
", age=" + age +
34+
'}';
35+
}
36+
37+
@Override
38+
public int compareTo(Student o) {
39+
return this.getAge() - o.getAge();
40+
}
41+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.oslee.algorithm.test;
2+
3+
import com.oslee.algorithm.sort.Bubble;
4+
5+
import java.util.Arrays;
6+
7+
8+
public class BubbleTest {
9+
10+
public static void main(String[] args) {
11+
Integer[] arr = {4, 5, 6, 3, 2, 1};
12+
Bubble.sort(arr);
13+
14+
System.out.println(Arrays.toString(arr));
15+
}
16+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.oslee.algorithm.test;
2+
3+
import com.oslee.algorithm.sort.Student;
4+
5+
/**
6+
* @version:
7+
* @description: 定义测试类Test, 在测试类Test中定义测试方法Comparable getMax(Comparable c1, Comparable c2)完成测试
8+
* @author: Lee
9+
* @create: 2020-03-04 22:28
10+
*/
11+
public class TestComparable {
12+
13+
public static void main(String[] args) {
14+
Student student1 = new Student();
15+
student1.setUsername("张三");
16+
student1.setAge(18);
17+
18+
Student student2 = new Student();
19+
student2.setUsername("李四");
20+
student2.setAge(20);
21+
22+
Comparable max = getMax(student1, student2);
23+
System.out.println(max.toString());
24+
}
25+
26+
public static Comparable getMax(Comparable c1, Comparable c2){
27+
int result = c1.compareTo(c2);
28+
// 如果result小于0, 则c1比c2小; 如果result小大0, 则c1比c2大; 如果result==0, 那么c1和c2一样大
29+
if (result >= 0){
30+
return c1;
31+
} else {
32+
return c2;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)