java -排序算法记录
import java.util.Arrays;
public class SomeSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 1, 5, 6, 2, 7, 4, 9 };// 升序
// selectionSort(a);
//bubbleSort(a);
insertSort(a);
System.out.println(Arrays.toString(a));
}
// 选择排序
public static void selectionSort(int[] a) {
for (int i = 0; i < a.length; i++) {
int min = a[i], temp;
for (int j = i; j < a.length; j++) {
if (min > a[j]) {
temp = min;
min = a[j];
a[j] = temp;
}
}
a[i] = min;
}
}
// 冒泡排序
public static void bubbleSort(int[] a) {
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
//插入排序
public static void insertSort(int[] a){
int temp;
for(int i = 1;i < a.length;i ++){
for(int j = i;j > 0;j --){
if(a[j] < a[j - 1]){
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
}
}
}
}
}
来源:https://www.cnblogs.com/dragonetyu/p/7591659.html