import java.util.Arrays;
public class Bubble {
//构造方法 Bubble():创建Bubble对象
// 成员方法
//public static void sort(Comparable[] a):对数组内的元素进行排序
//private static boolean greater(Comparable v,Comparable w):判断v是否大于w
//private static void exch(Comparable[] a,int i,int j):交换a数组中,索引i和索引j处的值
//public int compareTo(Integer anotherInteger)在数字上比较两个Integer对象 >返回1 =返回0 <返回-1
public static void selectSort(Comparable[] a){
for (int i = 0; i < a.length-1; i++) {
int min=i;
for (int j = i+1; j < a.length; j++) {
if (greater(a[min],a[j])){
min=j;
}
exch(a,i,min);
}
}
}
private static boolean greater(Comparable v,Comparable w){
return v.compareTo(w)>0;
}
private static void exch(Comparable[] a,int i,int j){
Comparable temp;
temp =a[i];
a[i]=a[j];
a[j]=temp;
}
public static void main(String[] args) {
Integer []a={1,3,5,2,4,6,7,0,-1,-1,-2};
selectSort(a);
System.out.println(Arrays.toString(a));
}
}
数据结构之选择排序(api设计实现)
最新推荐文章于 2023-04-08 14:26:34 发布