选择排序+冒泡排序
2021-04-02 13:26
标签:ati span div ++ for system str 选择排序 static 选择排序+冒泡排序 标签:ati span div ++ for system str 选择排序 static 原文地址:https://www.cnblogs.com/linding/p/13489099.htmlimport java.util.Arrays;
public class Arrsort {
public static void main(String[] args) {
int[] arr = {5, 8, 3, 4, 9, 2, 7, 6, 1};
System.out.println(Arrays.toString(bubblesort(arr)));
System.out.println(Arrays.toString(selectsort(arr)));
System.out.println(Arrays.toString(arr));
}
private static int[] bubblesort(int[] arr) {
int[] newarr = new int[arr.length];
System.arraycopy(arr, 0, newarr, 0, arr.length);
for (int i = newarr.length - 1; i > 0; i--) {
for (int j = 0; j ) {
if (newarr[j] > newarr[j + 1]) {
int temp = newarr[j];
newarr[j] = newarr[j + 1];
newarr[j + 1] = temp;
}
}
}
return newarr;
}
private static int[] selectsort(int[] arr) {
int[] newarr = new int[arr.length];
System.arraycopy(arr, 0, newarr, 0, arr.length);
for (int i = 0; i ) {
for (int j = i + 1; j ) {
if (newarr[i] newarr[j]) {
int temp = newarr[i];
newarr[i] = newarr[j];
newarr[j] = temp;
}
}
}
return newarr;
}
}