选择排序的实现代码
@SuppressWarnings("rawtypes")
public class SelectSort {
public static void sort(Comparable[] v) {
int size = v.length;
for (int i = 0; i < size; i++) {
int min = i;
for (int j = i + 1; j < size; j++) {
min = less(v[min], v[j]) ? min : j;
}
exchange(v, i, min);
}
}
public static boolean less(Comparable a, Comparable b) {
return a.compareTo(b) < 0;
}
public static void exchange(Comparable[] v, int a, int b) {
Comparable w = v[a];
v[a] = v[b];
v[b] = w;
}
public static boolean issorted(Comparable[] v) {
for (int i = 0; i < v.length - 1; i++) {
if (less(v[i + 1], v[i]))
return false;
}
return true;
}
public static void show(Comparable[] v){
StringBuffer sb=new StringBuffer();
for (int i = 0; i < v.length; i++) {
sb.append(" "+v[i]);
}
System.out.println(sb.toString());
}
public static void main(String[] args){
Double[] n={1.2,3.5,8.2,0.5,1.8,2.9,3.0,4.5};
sort(n);
show(n);
System.out.println(issorted(n));
}
}