用java实现选择排序
public class SelectionSort {
public static void selectionSort(double[]
list){
double currentMin;
int currentMinIndex;
for(int i = 0;i
< list.length; i++){
currentMin = list[i];
currentMinIndex =
i;
for(int j = i+1; j < list.length; j++){
if(currentMin
> list[j]){
currentMin = list[j];
currentMinIndex =
j;
}
}
if( currentMin != i){
list[currentMinIndex]
= list[i];
list[i] = currentMin;
}
}
}
}
用java实现插入排序
public class InsertionSort {
public static void InsertionSort(double[]
list){
for(int i = 1; i < list.length; i++){
double currentElement
= list[i];
int k ;
for(k = i-1; k>=0 && (list[k] >
currentElement); k-- ){
list[k+1] = list[k];
}
list[k+1] =
currentElement;
}
}
}
为实现数组的排序和查找,数组的比较和对数组填充元素,java.util.Arrays类包括了各种各样的静态方法,这些方法都有对所有基本类型的重载方法.
java.util.Arrays.sort()实现对数组的排序
java.util.Arrays.binarySearch()采用二分查找法在数组中查找关键字
java.util.Arrays.equals()用于检测两个数组是否相等
java.util.Arrays.fill()用于填充整个数组或部分数组