zoukankan      html  css  js  c++  java
  • 快排 l

    package mytest;
    
    import java.util.Arrays;
    
    /**
     * @author :l_coil
     * @date :2021/12/11 9:41 下午
     */
    public class QuickSort {
    
        public static void main(String[] args) {
            int[] ints = {3, 1, 4, 8, 7, 5, 2};
            int[] result = quickSort(ints,0,6);
            System.out.println(Arrays.toString(result));
        }
        public static int[] quickSort(int[] arr, int l, int r) {
    
            // 确定一次运算的基准值
            int base = arr[l];
            int lindex = l, rindex = r;
            while (lindex < rindex) {
                // 从右至左找到第一个小于基准值的位置
                while (arr[rindex] > base && lindex < rindex) {
                    rindex--;
                }
                // 从左至右找到第一个大于基准值的位置
                while (arr[lindex] < base && lindex < rindex) {
                    lindex++;
                }
    
                // 处于中间位置,一轮运算暂未结束,进行交换
                if (lindex < rindex && arr[lindex] != arr[rindex]) {
                    int temp = arr[rindex];
                    arr[rindex] = arr[lindex];
                    arr[lindex] = temp;
                }
                //向右移动左值指针,或者向左移动右指针都阔以
                if (lindex < rindex && arr[lindex] == arr[rindex]) {
                    lindex++;
                }
            }
    
            // 分治前一轮的再排序
            if (l < lindex - 1) {
                arr = quickSort(arr, l, lindex - 1);
            }
            if (rindex + 1 < r) {
                arr = quickSort(arr, rindex + 1, r);
            }
    
            return arr;
        }
    }
    

      

    本文来自博客园,作者:l-coil,转载请注明原文链接:https://www.cnblogs.com/l-coil/p/15678414.html

  • 相关阅读:
    python爬虫第二天
    sqlite3 数据库创建表
    python 中的nonlocal
    python中 random.seed()函数
    每日一题6/5
    竞赛191
    二进制操作, ~按位取反, | 或, & 与, ^异或, >倍数
    竞赛190
    css BFC
    css动画 Vs js动画
  • 原文地址:https://www.cnblogs.com/l-coil/p/15678414.html
Copyright © 2011-2022 走看看