zoukankan      html  css  js  c++  java
  • 快速排序算法

    链接:https://blog.csdn.net/weixin_43570367/article/details/102880632

    public class QuickSort {
        public static void main(String[] args) {
            int[] array = {-5,-11,6, 2, 4, 8, 9, 5, 7, 3, 1, 10};
            System.out.println("排序之前的数组: " + Arrays.toString(array));
            quickSort(array, 0, array.length - 1);
            System.out.println("排序之后的数组: " + Arrays.toString(array));
        }
    
        private static void quickSort(int[] array, int startIndex, int endIndex) {
            if (startIndex < endIndex) {
                // 找基准元素
                int baseIndex = divide(array, startIndex, endIndex);
                // 递归调用,对分隔后的左边数组快速排序
                quickSort(array, startIndex, baseIndex - 1);
                // 递归调用,对分隔后的右边数组快速排序
                quickSort(array, baseIndex + 1, endIndex);
            } else {
                return;
            }
        }
    
        /**
         * 利用双边循环法分隔数组
         *
         * @param array      需要排序的数组
         * @param startIndex 数组的开始下标
         * @param endIndex   数组的结束下标
         * @return 返回分隔点所在的位置
         */
        private static int divide(int[] array, int startIndex, int endIndex) {
            // 用数组的第一个元素作为起始元素
            int base = array[startIndex];
            int i = startIndex;
            int j = endIndex;
            while (i != j) {
                // 从右向左寻找第一个小于基准数的值
                while (i < j && array[j] > base) {
                    j--;
                }
                // 从左向右寻找第一个大于基准数的值
                while (i < j && array[i] <= base) {
                    i++;
                }
                // 交换位置
                if (i < j) {
                    swap(array, i, j);
                }
            }
    
            // 指针i 与指针j 相遇,把重合点的元素与基准元素交换位置
            array[startIndex] = array[i];
            array[i] = base;
    
            // 返回分隔点所在的位置
            return i;
        }
    
        /**
         * 交换i 与 j 位置的值
         *
         * @param array
         * @param i
         * @param j
         */
        private static void swap(int[] array, int i, int j) {
            int temp;
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }
    }

    简化后:

    public class QuickSortTest {
    
         public static void main(String[] args) {
                int[] array = {-5,-11,6, 2, 4, 8, 9, 5, 7, 3, 1, 10};
                System.out.println("排序之前的数组: " + Arrays.toString(array));
                quickSort(array, 0, array.length - 1);
                System.out.println("排序之后的数组: " + Arrays.toString(array));
            }
        
        public static int[] quickSort(int arr[],int start,int end) {
            int pivot = arr[start];
            int i = start;
            int j = end;
            while (i<j) {
                while ((i<j)&&(arr[j]>pivot)) {
                    j--;
                }
                while ((i<j)&&(arr[i]<pivot)) {
                    i++;
                }
                if ((arr[i]==arr[j])&&(i<j)) {
                    i++;
                } else {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
            if (i-1>start) arr=quickSort(arr,start,i-1);
            if (j+1<end) arr=quickSort(arr,j+1,end);
            return (arr);
        }
        
    }
  • 相关阅读:
    @responseBody注解的使用
    springmvc下的省市县三级联动
    select 动态添加option函数
    清空select标签中option选项的4种不同方式
    javascript删除option选项的多种方法总结
    js如何获取select下拉框的value以及文本内容
    如何设置select下拉禁止选择
    java.sql.SQLSyntaxErrorException: ORA-00911: 无效字符
    转:通过他人完成任务的艺术
    ***周鸿祎谈创业:很多程序员高智商 但我一看就知道他们不会成功
  • 原文地址:https://www.cnblogs.com/Steven5007/p/14816920.html
Copyright © 2011-2022 走看看