zoukankan      html  css  js  c++  java
  • 常见的数组排序方法

    import java.util.Arrays;
    
    public class mysort {
        public static void main(String[] args) {
            int[] arr = { 36, 51, 43, 12, 54, 68, 89, 53, 26, 5, 84, 9, 8, 4, 52 };
            System.out.println("原数组输出");
            for (int x : arr) {
                System.out.print(x + " ");
            }
            System.out.println();
            /*
             * System.out.println("冒泡排序结果:"); buddleSort(arr); System.out.println();
             * System.out.println("选择排序结果:"); selecttionSort(arr);System.out.println();
             */
            System.out.println("java自带排序方法:");
            Arrays.sort(arr);
            for (int x : arr) {
                System.out.print(x + " ");
            }
        }
    
        // 冒泡排序方法
        public static void buddleSort(int[] arr) {
    
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j + 1]) {
                        int a = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = a;
                    }
                }
            }
            for (int x : arr) {
                System.out.print(x + " ");
            }
        }
    
        // 选择排序方法
        public static void selecttionSort(int[] arr) {
            for (int i = 0; i < arr.length - 1; i++) {
                int k = i;
                for (int j = k + 1; j < arr.length; j++) {
                    if (arr[j] < arr[k]) {
                        k = j;
                    }
                }
                if (i != k) {
                    int temp = arr[i];
                    arr[i] = arr[k];
                    arr[k] = temp;
                }
            }
            for (int x : arr) {
                System.out.print(x + " ");
            }
        }
    }
  • 相关阅读:
    Swift
    美国绿卡
    H-1-B签证简介
    托福、雅思和GRE的区别
    使用fdisk命令对linux硬盘进行操作
    Welcome to Workrave
    Installing patches on an ESXi 5.x by the command
    How to install VIB on VMware ESXi
    Robocopy用法
    Location of ESXi 5.1 log files
  • 原文地址:https://www.cnblogs.com/ddaifenxiang/p/10135832.html
Copyright © 2011-2022 走看看