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

    快速排序原理还是简单的,先确定一个基准数,最左或最右边的数,左边都比基准数小,右边大,然后交换基准数和移动后的左标(右标位置)继续直到不能进行下去

    import java.awt.List;
    
    class Sort{
        public void run(int[] list) {
            qsort(list, 0, list.length-1);
            for (int i = 0; i < list.length; i++) {
                System.out.println(list[i]);
            }
        }
        
        public int getMiddle(int[] list, int low, int high) {
            int base = list[low];
            int left = low;//上一次左标
            
            while (low < high) {
                while(low < high && list[high] >= base) {
                    high--;
                }
                while(low < high && list[low] <= base) {
                    low++;
                }
                int temp = list[low];
                list[low] = list[high];
                list[high] = temp;
            }
            list[left] = list[low];
            list[low] = base;
            
            return low;
        }
        
        public void qsort(int[] list, int low, int high) {
            
            if(low < high) {
                int middle = getMiddle(list, low, high);
                qsort(list, low, middle - 1);
                qsort(list, middle + 1, high);
            }
             
            
        }
        
    }
    
    
    
    
    
    
    
    
    public class QuickSort {
        public static void main(String[] args) {
            int []num = {3,56,45,98,23,5,81,56,90,90};
            Sort sort = new Sort();
            sort.run(num);
        }
    }

    有一点注意!如果基准数取最左边,一定要先移动右标(high)再左标(low)

    反之相反。

  • 相关阅读:
    python 集合 set
    Meet Python
    Python 模块
    KNN
    Python Numpy包安装
    R分词
    Maximum Entropy Model(最大熵模型)初理解
    Conditional Random Fields (CRF) 初理解
    Naive Bayes (NB Model) 初识
    Hidden Markov Models(HMM) 初理解
  • 原文地址:https://www.cnblogs.com/shineyoung/p/10476259.html
Copyright © 2011-2022 走看看