1:获得数组的最大最小值
int[] array = { 8, 2, 3, 1 };
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int value : array) {
if (min > value) {
min = value;
}
if (max < value) {
max = value;
}
}
1:冒泡排序
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arry = { 4, 3, 2, 1,213,24,0,12321 };
boolean flag = true;
int endIndex = arry.length;
while (flag) {
int i;
flag = false;
for (i = 0; i < endIndex-1; i++) {
if (arry[i] > arry[i + 1]) {
int temp = arry[i + 1];
arry[i+1] = arry[i];
arry[i] = temp;
flag = true;
}
}
endIndex = i;
}
System.out.println(Arrays.toString(arry));
}
}
2:插入排序
import java.util.Arrays;
public class InsertSort {
public static void main(String[] args) {
int[] arry = { 4, 3, 2, 1, 213, 24, 0, 12321 };
for (int i = 1; i < arry.length; i++) {
for (int j = i; j > 0 && arry[j-1] > arry[j]; j--) {
int temp=arry[j];
arry[j]=arry[j-1];
arry[j-1]=temp;
}
}
System.out.println(Arrays.toString(arry));
}
}
3:快速选择排序
public class SelectSort {
public static void main(String[] args) {
int[] arry = { 2, 213, 234, 1, 3, 3, 6 };
for (int i = 0; i < arry.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arry.length; j++) {
if (arry[minIndex] > arry[j]) {
int temp = arry[j];
arry[j] = arry[minIndex];
arry[minIndex] = temp;
}
}
}
System.out.println(Arrays.toString(arry));
}
}