一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 
import 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 < i; 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 < newarr.length; i++) {
            for (int j = i + 1; j < newarr.length; j++) {
                if (newarr[i] < newarr[j]) {
                    int temp = newarr[i];
                    newarr[i] = newarr[j];
                    newarr[j] = temp;
                }
            }
        }

        return newarr;
    }
}

 

posted on 2020-08-12 10:19  一路繁花似锦绣前程  阅读(143)  评论(0编辑  收藏  举报