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

        拆分数组,以最右侧为轴,小于轴的元素数组左侧,大于轴的元素数组右侧,将轴元素移到中央。

    public class QuickSort {
        public static void main(String[] args) {
            int[] arr = {7, 3, 2, 8, 1, 9, 5, 4, 6};
            sort(arr, 0, arr.length - 1);
            print(arr);
        }
    
        /**
         * 快速排序
         *
         * @param arr
         */
        private static void sort(int[] arr, int leftBound, int rightBound) {
            partition(arr, leftBound, rightBound);
        }
    
        private static void partition(int[] arr, int leftBound, int rightBound) {
            int pivot = arr[rightBound];// 杠杆元素,最右侧元素
            int left = leftBound; // 左侧开始元素
            int right = rightBound - 1; // 右侧开始元素
            while (left < right) {
                while (left < right && arr[left] <= pivot) {
                    left++;
                }
                while (left < right && arr[right] >= pivot) {
                    right--;
                }
                if (left < right) {
                    swap(arr, left, right);
                }
            }
            swap(arr, left, rightBound);
        }
    
        private static void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        private static void print(int[] arr) {
    
            for (int i : arr) {
                System.out.print(i + " ");
            }
        }
    }
    

      加入递归处理

    package com.example.sort.quick;
    
    public class QuickSort {
        public static void main(String[] args) {
            int[] arr = {7,3,2,10,8,1,9,5,4,6};
            sort(arr, 0, arr.length - 1);
            print(arr);
        }
    
        /**
         * 快速排序
         *
         * @param arr
         */
        private static void sort(int[] arr, int leftBound, int rightBound) {
            if (leftBound >= rightBound) return;
            int mid = partition(arr, leftBound, rightBound);
            sort(arr, leftBound, mid - 1);
            sort(arr, mid + 1, rightBound);
        }
    
        private static int partition(int[] arr, int leftBound, int rightBound) {
            int pivot = arr[rightBound];// 杠杆元素,最右侧元素
            int left = leftBound; // 左侧开始元素
            int right = rightBound - 1; // 右侧开始元素
            while (left <= right) {
                while (left <= right && arr[left] <= pivot) {
                    left++;
                }
                while (left <= right && arr[right] > pivot) {
                    right--;
                }
                if (left < right) {
                    swap(arr, left, right);
                }
            }
            swap(arr, left, rightBound);
            return left;
        }
    
        private static void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        private static void print(int[] arr) {
    
            for (int i : arr) {
                System.out.print(i + " ");
            }
        }
    }
    

      java默认排序,双轴快排

      ...

      

  • 相关阅读:
    根据判断PC浏览器类型和手机屏幕像素自动调用不同CSS的代码
    c#抓取网页内容乱码的解决方案
    C#中使用正则表达式提取超链接地址的集中方法
    sql server日期时间转字符串
    DataGridView直接导出EXCEL
    sql数据库删除表的外键约束(INSERT 语句与 FOREIGN KEY 约束"XXX"冲突。该冲突发生于数据库"XXX",表"XXX", column 'XXX)
    C#抓取页面时候,获取页面跳转后的地址
    HttpWebRequest 抓取页面异常处理办法
    JQuery的Ajax跨域请求的解决方案
    mysql 事物ACID和隔离级别
  • 原文地址:https://www.cnblogs.com/huan30/p/12840160.html
Copyright © 2011-2022 走看看