码迷,mamicode.com
首页 > 编程语言 > 详细

选择排序+冒泡排序

时间:2020-08-12 15:54:29      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:ati   span   div   ++   for   system   str   选择排序   static   

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;
    }
}

 

选择排序+冒泡排序

标签:ati   span   div   ++   for   system   str   选择排序   static   

原文地址:https://www.cnblogs.com/linding/p/13489099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!