zoukankan      html  css  js  c++  java
  • Java数组的排序算法

    在Java中,实现数组的排序算法有很多,如冒泡排序法、选择排序法、直接插入法和快速排序法等。下面介绍几种排序算法的具体 实现。

      本文引用文献:Java必须知道的300个问题。

    1.冒泡排序法

      1.1 基本思想:

        比较待排序的数据元素中的相邻元素:如果前面的元素大于后面的元素,那么将两个元素交换位置;否则不变。即:永远保持大的元素值在待排序元素中的最后面位置。这样,数组元素就像气泡一样从底部上升到顶部。

      1.2 过程实例:

        每一轮,排序数组的长度减1次(每一轮结束后,最大元素都是最后一个元素。因此下轮比较过程中最后一次比较不用进行。)

      1.3 代码实现

    复制代码
    public static void main(String[] args) {
        //初始化数组
        int[] array = {63,4,24,1,3,13};
        //排序
        for (int i = 1; i < array.length; i++) {
            for (int j = 0; j < array.length - i; j++) {
                if(array[j] > array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;  
                }
            }
        }
        //输出结果
        System.out.println(Arrays.toString(array));
    }
    复制代码

    2. 选择排序法

      2.1 基本思想

        每一轮从待排序的数据元素中选出最小(最大)的一个元素,将该元素与待排序的数据元素的最前面(最后面)的元素进行交换,直到全部待排序的数据元素排完。  

      2.2 过程实例  

       2.3 代码实现

    复制代码
    //初始化数组
    int[] array = {63,4,24,1,3,13};
    
    //排序
    int len = array.length;
    //控制轮数
    for (int i = 1; i < len; i++) {
        int max = array[0];
        int index = 0;
        //查找最大值
        for (int j = 1; j < len - (i - 1); j++) {
            if(max < array[j]){
                max = array[j]; 
                index = j;
            }
        }
        //互换位置
        int temp = array[index];
        array[index] = array[len - i];
        array[len - i] = temp;
    }
    
    //输出
    System.out.println(Arrays.toString(array));
    复制代码

    3. 直接插入排序法

      3.1 基本思想

        1. 将n个有序元素放在数组中 --> 2.确认要插入元素的位置 --> 3.将数组中的要插入元素的位置后面的元素向后移一个位置  --> 4.将要出如的元素插到合适的位置上 --> 5.重复2. 3. 4.直到所有元素均插入到数组中。

      3.2 实例过程

        

      3.3 代码实现

        

    复制代码
    //初始化数组
    int[] array = {20,40,90,30,80,70,50};
    
    //排序
    int j;
    for (int i = 1; i < array.length; i++) {
        int temp = array[i];
        for (j = i - 1; j > 0 && array[j] > temp; j--) {
            array[j+1] = array[j];
        }
        array[j+1] = temp;
    }
    
    //输出
    System.out.println(Arrays.toString(array));
    复制代码

    4. 快速排序法

       4.1 基本思想

        通过一趟排序将要排序的数据分割成独立的两部分(通常选取中数作为分割线),其中一部分的所有数据都比另一部分的所有数据要小,然后再按此方法对这两部分数据进行快速排序,整个过程可以通过递归进行。

      4.2 实例过程

      4.3 代码实现

    复制代码
    public static void main(String[] args) {
        //初始化数组
        int[] array = {49,38,65,97,76,13,27,49};
            
        //排序
        quickSort(array, 0, array.length - 1);
            
        //输出
        System.out.println(Arrays.toString(array));
            
    }
    
    private static void quickSort(int[] array, int lowIndex, int highIndex) {
        int lo = lowIndex;
        int hi = highIndex;
        int mid;
        if(highIndex > lowIndex){
            mid = array[(lowIndex + highIndex) / 2];
            while(lo <= hi){
                while((lo < highIndex) && (array[lo] < mid)){
                    ++lo;
                }
                while(hi > lowIndex && array[hi] >mid){
                    --hi;
                }
                if(lo <= hi){
                    swap(array,lo,hi);
                    ++lo;
                    --hi;
                }
            }
            if(lowIndex <hi){
                quickSort(array, lowIndex, hi);
            }
            if(lo < highIndex){
                quickSort(array, lo, highIndex);
            }
        }
    }
    
    private static void swap(int[] array, int lo, int hi) {
        int temp = array[lo];
        array[lo] = array[hi];
        array[hi] = temp;
    }
    复制代码
  • 相关阅读:
    sql中where和having的区别
    mysql中locate和substring函数使用
    使用jdk进行数据迁移(sqlite迁移mysql)
    mysql数值函数
    mysql字符串函数
    zabbix-2.2.2(Ubuntu 14.04 LTS/OpenLogic 7.2)
    Piwik-2.16.1 (OpenLogic CentOS7.2)
    Nagios-4.1.1 (OpenLogic CentOS 7.2)
    Bugzilla-5.0.3 (OpenLogic CentOS 7.2)
    GitLab-CE-8.9.4 (OpenLogic CentOS 7.2)
  • 原文地址:https://www.cnblogs.com/XuYiHe/p/6862011.html
Copyright © 2011-2022 走看看