说到排序,生活中到处都少不了它。当然,我们的编程语言里面也要经常用到它。现在,我们可以看到下面很常见的冒泡排序法。(JAVA)这里我选的示例是单纯的从大到小排序。
public void bubble(int[] arr){
for(int i = 1 ; i < arr.length; i++ )
for(int j = 0 ; j < arr.length - i ; j++ )
if(arr[j] < arr[j + 1]){
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
随便拉个数组,我们看看结果:
int a[] = {4,5,2,7,9,3};
毫无疑问,排序确实完成了,我们也看到了结果。
但是,这个排序有一个严重的缺陷!就是……我想要别的类型呢?
比如说,双精度?比如说浮点数?我还要一个一个写代码?
再或者?什么自定义类?
一个个都写了,我还专门写个排序的类有什么意思?
而且每个都写一个,改动又十分麻烦,这种操作十分不符合我们写程序的初衷。我们写程序总不能到后面有一点风吹草动就要改到想死吧……于是乎,这语言也注意到这点,我们可以通过规范化的写法来完成排序。这就用到了一个名叫“接口”的很棒的东西。(注意关键字:interface implements)
一步步来,我们先试用下系统提供的接口:
public void bubble(Comparable[] arr){
for(int i = 1 ; i < arr.length; i++ )
for(int j = 0 ; j < arr.length - i ; j++ )
if(arr[j].compareTo(arr[j + 1]) == -1){
Comparable temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
public static void main(String[] args) {
Integer[] a = {1,5,8,7,9,3,2,4};
Double[] b = {1.1,2.4,3.5,4.6,6.7};
Soft soft = new Soft();
soft.bubble(a);
soft.bubble(b);
for(Integer i : a)
System.out.print(i + " ");
System.out.println();
for(Double i : b)
System.out.print(i + " ");
}
接着,出现了我们都求着要的一幕。一个函数,硬生生的给了两个数据类型排序(这里的Integer和Double相当于是规范化过后的int和double,也可以这么看)
但是,仅仅是这些类型还不够啊,我们还想要弄更多的类型怎么办?比如说,我们弄个学生类试试?
public class Student{
private int num;
private String name;
private String sex;
public Student(int num, String name, String sex) {
this.num = num;
this.name = name;
this.sex = sex;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
紧接着,我们就获得了一个报错奖励……
What?发生了什么?之前不是可以吗?为什么现在就不行了?这是不是这个世界的恶意,太诡异了!
为什么前面两个可以这里就不可以了?
原因嘛……我们在之前函数里面需要传入带有Comparable接口的类,这个类显然不行。必须通过此接口来完成里面的统一的比较大小的操作方式。而Integer和Double本身就是别人已经规定好的类,什么东西都写好了,自然就可以直接这样用了,而我们写的Student类,里面很多东西还没写,所以不行。
前面两个是系统自定义好的东西,本身就有这个什么compareTo但是我们的写的本来是没有的,所以自然不行了。
好了,现在没有报错,仅仅这样就排序出来了:
Student[] s = {
new Student(1,"老大是我","男"),
new Student(2,"我居然成了老二","男"),
new Student(3,"嗯……","女"),
new Student(4,"这情况不对","女")
};
Soft soft = new Soft();
soft.bubble(s);
for(Student i : s)
System.out.println("Student{" +
"num=" + i.getNum() +
", name='" + i.getName() + '\'' +
", sex='" + i.getSex() + '\'' +
'}');
你没有看错,依然是之前的那个函数,就这么直接的完成了排序:
但是,到这里又会有人对此不太喜欢了,因为,这东西分明就是只是冲着降序来排的,一点没顾及什么最想要的效果。
到这里,我想说,其实修改并没有这么难:
public void bubble(Comparable[] arr , int softBy){
//设置是升序还是降序 其中0为升序,1以及剩下的为降序
softBy = (softBy == 0)? -1 : 1;//这里只是稍微做点调整而已
for(int i = 1 ; i < arr.length ; i++ )
for(int j = 0 ; j < arr.length - i ; j++ )
if(arr[j].compareTo(arr[j + 1]) == softBy){
Comparable temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
// 至于下面唯一的修改了的地方就是 soft.bubble(s,0);
依然是相同的答案
soft.bubble(s,1);
//稍微改变了下位置
Student[] s = {
new Student(1,"老大是我","男"),
new Student(3,"嗯……","女"),
new Student(2,"我居然成了老二","男"),
new Student(4,"这情况不对","女")
};
依然是这么样可以排序。
不过有人觉得这么样,单单只是能给学号排序,感觉就是不够好……于是,现在该是我们介绍自己写的interface的时候了。
public interface Compare {
public int myCompare(Object o,int compareData);
}
//implements Comparable,Compare这里我多加了一个自己写的接口类代码
private int num;
private int score;
private String name;
private String sex;
public int myCompare(Object o,int compareData){
Student s = (Student)o;
switch(compareData){
case 0:
if(this.num > s.num) return 1;
else if(this.num < s.num) return -1;
return 0;
case 1:
if(this.score > s.score) return 1;
else if(this.score < s.score) return -1;
return 0;
default:return -2;//错误现象,不可能有更多的比较
}
}
为了能够实验上述所说的,我特别加了一个score比较。(主要是觉得字符串规则订起来不好定,在这里排大小也没意思)
/**
* @param arr 这个数组我不太想解释什么
* @param softBy 设置是升序还是降序 其中0为升序,1以及剩下的为降序
* @param softData 是我们规定的需要的第几个排序,比如说规定学生里面的学号,这里便比较学号,否则会比较别的数据
*/
public void bubble(Compare[] arr , int softBy , int softData){
softBy = (softBy == 0)? -1 : 1;
for(int i = 1 ; i < arr.length ; i++ )
for(int j = 0 ; j < arr.length - i ; j++ )
if(arr[j].myCompare(arr[j + 1],softData) == softBy){
Compare temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
接着这里可以看到我们排序的程序,接着我们来看看我们的输入
Student[] s = {
new Student(1,67,"老大是我","男"),
new Student(3,77,"嗯……","女"),
new Student(2,83,"我居然成了老二","男"),
new Student(4,69,"这情况不对","女")
};
Soft soft = new Soft();
soft.bubble(s,1,0);
for(Student i : s)
System.out.println("Student{" +
"num=" + i.getNum() +
"score=" + i.getScore() +
", name='" + i.getName() + '\'' +
", sex='" + i.getSex() + '\'' +
'}');
System.out.println();
soft.bubble(s,0,1);
for(Student i : s)
System.out.println("Student{" +
"num=" + i.getNum() +
"score=" + i.getScore() +
", name='" + i.getName() + '\'' +
", sex='" + i.getSex() + '\'' +
'}');
我们这里同样是调用这个函数,第一次是相当于学号排序升序,第二次是相当于分数排行降序
结果如下:
结果出来了,也是符合我们的意思。
好了,我想到的大概就这么多,如果你有耐心看到这里,感谢支持!