• 排序


    /**
    *
    */
    package org.adaikiss.algorithm.sort;

    import org.apache.commons.lang.ArrayUtils;

    /**
    * 排序算法
    *
    *
    @author hlw
    *
    */
    public class Sort {
    /**
    * 冒泡排序O(n^2) 从第一个元素A开始,和后面元素逐个比较,若比A小, 则A和后面元素换位,直到和最后一个元素比较完,
    * 这样之后第一个元素是最小的,后面元素重复 以上步骤。
    *
    *
    @param array
    *
    @return
    */
    public static int[] bubbleSort(int[] array) {
    for (int i = 0; i < array.length; i++)
    for (int j = i + 1; j < array.length - 1; j++)
    if (array[i] > array[j]) {
    int temp = array[i];
    array[i]
    = array[j];
    array[j]
    = temp;
    }
    return array;
    }

    /**
    * 直接插入排序O(n^2) 假设选中元素A前面一个子集是有序递增的(这里选第二个元素array[1]),
    * 用temp保存A,拿temp和A前面的元素逐个比较,若比A大,则将A前面的值
    * 放到A中,再拿temp和前一个值比较,以此类推,直到找到第一个比A小的 元素B,那么在B之前的元素一定都比A要小,故将A插入到B之后。
    * 再选A后面一个元素重新开始以上步骤。
    *
    *
    @param array
    *
    @return
    */
    public static int[] insertionSort(int[] array) {
    for (int i = 1; i < array.length; i++) {
    int temp = array[i];
    array[i]
    = array[i - 1];
    for (int j = i - 1; j >= 0; j--) {
    if (array[j] > temp) {
    array[j
    + 1] = array[j];
    }
    else {
    array[j
    + 1] = temp;
    break;
    }
    }
    }
    return array;
    }

    /**
    * 选择排序O(n^2) 假设第一个元素是最小的,比较出最小的,然后和第一个元素置换, 再从第二个元素开始,重复。
    *
    *
    @param array
    *
    @return
    */
    public static int[] selectionSort(int[] array) {
    for (int i = 0; i < array.length; i++) {
    int min = i;
    for (int j = i + 1; j < array.length; j++) {
    if (array[min] > array[j])
    min
    = j;
    }
    if (min != i) {
    int temp = array[min];
    array[min]
    = array[i];
    array[i]
    = array[temp];
    }
    }
    return array;
    }

    /**
    * 快速排序O(n log n) 取一个元素,将该元素左边和右边的元素逐个和他比较, 找到左边开始第一个比它大,右边开始第一个比它小的
    * 元素,这两个元素置换,然后再找下一对,直到左边开 始和右边开始的序号相同,再分别对左右子集进行以上 的递归操作
    *
    *
    @param array
    *
    @return
    */
    public static int[] quickSort(int[] array) {
    int left = 0;
    int right = array.length - 1;
    sort(array, left, right);
    return array;
    }

    public static void sort(int[] array, int left, int right) {
    int i = left;
    int j = right;
    // 用来比较的中间值
    int middle = array[(left + right) / 2];
    while (i < j) {
    // i一直自增直到array[i]>=middle或i==right
    while (array[i] < middle && i < right)
    i
    ++;
    // j一直自减直到array[j]<=middle或j==left
    while (array[j] > middle && j > left)
    j
    --;
    if (i <= j) {
    // 这时有array[i]>=middle,array[j]<=middle
    // 将array[i]和array[j]置换
    int temp = array[i];
    array[i]
    = array[j];
    array[j]
    = temp;
    // i递增,j递减,进行下一次比较
    i++;
    j
    --;
    }
    }
    // 当j还没有到最左边,i还没有到最右边,进行递归操作
    if (left < j)
    sort(array, left, j);
    if (right > i)
    sort(array, i, right);
    }

    public static void print(int[] array) {
    for (int i : array)
    System.out.print(i
    + " ");
    System.out.println();
    }

    /**
    *
    @param args
    */
    public static void main(String[] args) {
    int[] array = new int[] { 1, 8, 3, 12, 55, 73, 11, 96 };
    print(array);
    print(bubbleSort(ArrayUtils.clone(array)));
    print(insertionSort(ArrayUtils.clone(array)));
    print(quickSort(ArrayUtils.clone(array)));
    }

    public void s(int[] array, int left, int right) {
    int i = left, j = right;
    int middle = array[(left + right) >>> 1];
    while (i < j) {
    while (array[i] < middle&&i<right)
    i
    ++;
    while (array[j] > middle&&j>left)
    j
    --;
    if(i<=j){
    int temp = array[i];
    array[i]
    = array[j];
    array[j]
    = temp;
    i
    ++;
    j
    ++;
    }
    }
    if(left<j)
    s(array, left, j);
    if(right>i)
    s(array, i, right);
    }
    }
    /**
     *
     */
    package org.adaikiss.algorithm.sort;

    import org.apache.commons.lang.ArrayUtils;

    /**
     * 排序算法
     *
     * @author hlw
     *
     */
    public class Sort {
        /**
         * 冒泡排序O(n^2) 从第一个元素A开始,和后面元素逐个比较,若比A小, 则A和后面元素换位,直到和最后一个元素比较完,
         * 这样之后第一个元素是最小的,后面元素重复 以上步骤。
         *
         * @param array
         * @return
         */
        public static int[] bubbleSort(int[] array) {
            for (int i = 0; i < array.length; i++)
                for (int j = i + 1; j < array.length - 1; j++)
                    if (array[i] > array[j]) {
                        int temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
            return array;
        }

        /**
         * 直接插入排序O(n^2) 假设选中元素A前面一个子集是有序递增的(这里选第二个元素array[1]),
         * 用temp保存A,拿temp和A前面的元素逐个比较,若比A大,则将A前面的值
         * 放到A中,再拿temp和前一个值比较,以此类推,直到找到第一个比A小的 元素B,那么在B之前的元素一定都比A要小,故将A插入到B之后。
         * 再选A后面一个元素重新开始以上步骤。
         *
         * @param array
         * @return
         */
        public static int[] insertionSort(int[] array) {
            for (int i = 1; i < array.length; i++) {
                int temp = array[i];
                array[i] = array[i - 1];
                for (int j = i - 1; j >= 0; j--) {
                    if (array[j] > temp) {
                        array[j + 1] = array[j];
                    } else {
                        array[j + 1] = temp;
                        break;
                    }
                }
            }
            return array;
        }

        /**
         * 选择排序O(n^2) 假设第一个元素是最小的,比较出最小的,然后和第一个元素置换, 再从第二个元素开始,重复。
         *
         * @param array
         * @return
         */
        public static int[] selectionSort(int[] array) {
            for (int i = 0; i < array.length; i++) {
                int min = i;
                for (int j = i + 1; j < array.length; j++) {
                    if (array[min] > array[j])
                        min = j;
                }
                if (min != i) {
                    int temp = array[min];
                    array[min] = array[i];
                    array[i] = array[temp];
                }
            }
            return array;
        }

        /**
         * 快速排序O(n log n) 取一个元素,将该元素左边和右边的元素逐个和他比较, 找到左边开始第一个比它大,右边开始第一个比它小的
         * 元素,这两个元素置换,然后再找下一对,直到左边开 始和右边开始的序号相同,再分别对左右子集进行以上 的递归操作
         *
         * @param array
         * @return
         */
        public static int[] quickSort(int[] array) {
            int left = 0;
            int right = array.length - 1;
            sort(array, left, right);
            return array;
        }

        public static void sort(int[] array, int left, int right) {
            int i = left;
            int j = right;
            // 用来比较的中间值
            int middle = array[(left + right) / 2];
            while (i < j) {
                // i一直自增直到array[i]>=middle或i==right
                while (array[i] < middle && i < right)
                    i++;
                // j一直自减直到array[j]<=middle或j==left
                while (array[j] > middle && j > left)
                    j--;
                if (i <= j) {
                    // 这时有array[i]>=middle,array[j]<=middle
                    // 将array[i]和array[j]置换
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                    // i递增,j递减,进行下一次比较
                    i++;
                    j--;
                }
            }
            // 当j还没有到最左边,i还没有到最右边,进行递归操作
            if (left < j)
                sort(array, left, j);
            if (right > i)
                sort(array, i, right);
        }

        public static void print(int[] array) {
            for (int i : array)
                System.out.print(i + " ");
            System.out.println();
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
            int[] array = new int[] { 1, 8, 3, 12, 55, 73, 11, 96 };
            print(array);
            print(bubbleSort(ArrayUtils.clone(array)));
            print(insertionSort(ArrayUtils.clone(array)));
            print(quickSort(ArrayUtils.clone(array)));
        }

        public void s(int[] array, int left, int right) {
            int i = left, j = right;
            int middle = array[(left + right) >>> 1];
            while (i < j) {
                while (array[i] < middle&&i<right)
                    i++;
                while (array[j] > middle&&j>left)
                    j--;
                if(i<=j){
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                    i++;
                    j++;
                }
            }
            if(left<j)
                s(array, left, j);
            if(right>i)
                s(array, i, right);
        }
    }
  • 相关阅读:
    Nginx Backup配置
    CANas分析软件,DBC文件解析,CAN报文分析,仿CANoe曲线显示
    MySQL 报错:MySQL Illegal mix of collations for operation 'like'
    docker部署rebbitmq
    docker部署kafka
    nodejs 环境配置
    spring是怎么运行的?
    Java发展的时间表
    单例模式(转)
    disable的错误使用
  • 原文地址:https://www.cnblogs.com/adaikiss/p/1811871.html
Copyright © 2020-2023  润新知