选择排序:selection sort
public class SelectionSort {
public static void selectionSort (double []list){
for (int i=0 ;i<list.length-1 ;i++) {
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i; j < list.length; j++)
if (currentMin > list[j]) {
currentMin = list[j];
currentMinIndex = j;
}
if (currentMinIndex!=i){
list[currentMinIndex]=list[i];
list[i]=currentMin;
}
}
}
}
插入排序:insertion sort
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;
}
}
}
冒泡排序
public class BubbleSort {
public void bubbleSort (int [] list) {
for (int i = 0 ; i < list.length - 1 ; i++) {
for (int j = i + 1 ; j < list.length; j++) {
int temp;
if (list[i] > list[j]) {
temp = list[j];
list[j] = list[i];
list[i] = temp;
}
}
}
}