package com.song.boot.springstudy.sort;
public class Insert {
public static void main(String[] args) {
int[] is = {8, 3, 9, 2, 1, 6, 5};
System.out.println("-------------");
System.out.println("冒泡");
System.out.println("-------------");
System.out.println("选择");
select(is);
}
private static void insertSort(int[] ints) {
for (int i = 1; i < ints.length; i++) {
int tmp;
for (int j = 0; j < ints.length; j++) {
if (ints[i] < ints[j]) {
tmp = ints[i];
ints[i] = ints[j];
ints[j] = tmp;
}
}
}
for (int i = 0; i < ints.length; i++) {
System.out.println(ints[i]);
}
}
private static void bubbleSort(int[] ints) {
int tmp;
for (int j = 0; j < ints.length - 1; j++) {
for (int i = 0; i < ints.length - 1; i++) {
if (ints[i] > ints[i + 1]) {
tmp = ints[i];
ints[i] = ints[i + 1];
ints[i + 1] = tmp;
}
}
}
for (int i = 0; i < ints.length; i++) {
System.out.println(ints[i]);
}
}
private static void select(int[] ints) {
for (int j = 0; j < ints.length - 1; j++) {
int k = j;
int tmp;
for (int i = j + 1; i < ints.length - 1; i++) {
if (k > ints[i + 1]) {
k =ints[i + 1];
}
tmp = ints[j];
ints[j] = ints[k];
ints[k] = tmp;
}
}
for (int i = 0; i < ints.length; i++) {
System.out.println(ints[i]);
}
}
}
|