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

    1、Java语言实现

    抽象类

    public abstract class Sorter {
        public abstract void sort(int [] array);
    }

    实现类

    public class QuickSorter extends Sorter {
        @Override
        public void sort(int[] array) {
            quickSort(array, 0, array.length - 1);
        }
    
        private void quickSort(int[] array, int low, int high) {
            int pivotPos;//划分基准元素索引
            if (low < high) {
                pivotPos = partition(array, low, high);
                quickSort(array, low, pivotPos - 1);//左划分递归快速排序
                quickSort(array, pivotPos + 1, high);//右划分递归快速排序
            }
        }
    
        /**
         * 简单划分方法
         *
         * @param array
         * @param i
         * @param j
         * @return
         */
        private int partition(int[] array, int i, int j) {
            Integer pivot = array[i];//初始基准元素,如果过quickSort方法第一次调用,那么pivot初始为数组第一个元素
            while (i < j) {//两个指针从两侧向中间靠拢,不能相交
                //右侧指针向左移动
                while (j > i && array[j] >= pivot) {
                    j--;
                }
                if (i < j) {//如果在i和j没有相遇的情况下,找到了array[j] >= 基准元素pivot
                    array[i] = array[j];
                }
                //左侧指针向右移动
                while (i < j && array[i] <= pivot) {
                    i++;
                }
                if (i < j) {//如果在没有使i和j相交的情况下找到array[i] <= 基准元素pivot
                    array[j] = array[i];//基准元素放到i指针处
                    j--;//右侧j指针需要向左移动一个位置
                }
            }
            array[i] = pivot;//将基准元素放到正确的排序位置上
            return i;
        }
    
    }

    测试

    public class Test {
        public static void main(String[] args) {
            int[] arr = {94, 12, 34, 76, 26, 9, 0, 37, 55, 76, 37, 5, 68, 83, 90, 37, 12, 65, 76, 49};
    
            Sorter sorter = new QuickSorter();
            sorter.sort(arr);
    
            System.out.println(Arrays.toString(arr));
    
        }
    }

     2、python语言实现

    from abc import ABCMeta, abstractmethod
    
    
    class Sorter:
        __metaclass__ = ABCMeta
    
        @abstractmethod
        def sort(self, array):
            pass
    
    class QuickSorter(Sorter):
        def sort(self, array):
            length = len(array)
            self.__quick_sort(array, 0, length - 1)
    
        def __quick_sort(self, array, low, high):
            if low < high:
                pivotPoint = self.__partition(array, low, high)
                self.__quick_sort(array, low, pivotPoint - 1)
                self.__quick_sort(array, pivotPoint + 1, high)
    
        def __partition(self, array, i, j):
            pivot = array[i]
            while i < j:
                while j > i and array[j] >= pivot:
                    j = j - 1
                if j > i:
                    array[i] = array[j]
                    i = i + 1
                while i < j and array[i] <= pivot:
                    i = i + 1
                if i < j:
                    array[j] = array[i]
                    j = j + 1
            array[i] = pivot
            return i;
    
    
    #测试排序效果
    if __name__ == '__main__':
        arr = [5, 4, 2, 1, 3]
        print(arr)
        _quickSorter =  QuickSorter()
        _quickSorter.sort(arr)
        print(arr)
  • 相关阅读:
    ajax上传文件
    nginx location指令详解
    总结php删除html标签和标签内的内容的方法
    useBuiltIns: 'usage'
    uni-app如何页面传参数的几种方法总结
    基于 schema 的数据校验
    canvas时点击事件和长按冲突
    vue 下载文件流,后台是get方式 ,并且导出出现excel乱码问题
    uni-app canvas 实现文字居中
    git reflog 回退
  • 原文地址:https://www.cnblogs.com/beanbag/p/9672844.html
Copyright © 2011-2022 走看看