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

    package com.demo02;
    
    import java.util.Arrays;
    
    /**
     * 快速排序
     */
    public class QuickSort {
        public static void main(String[] args) {
             int[] arr=new int[]{4,123,213,21,3241,3231,32,3221,1};
             quickSort(arr,0,arr.length-1);
            System.out.println(Arrays.toString(arr));
        }
        
        public static void quickSort(int[] array,int start,int end){
            if (start<end){
                //标准数
            int stard=array[start];
            //记录需要排序的下标
            int low=start;
            int high=end;
            //循环找比标准数大的数 和比标准数小的数
            while (low<high){
                //看右边的数是否比标准数大
                while (low<high&&array[high]>=stard){
                    high--;
                }
                array[low]=array[high];
                //看左边的数字是否比标准数小
                while (low<high&&array[low]<=stard){
                    low++;
                }
                array[high]=array[low];
            }
            //处理完后需要把标准数赋给所在位置的元素
            array[low]=stard;
            //处理所有小的数字
            quickSort(array,start,low);
            //处理所有大的数字
            quickSort(array,low+1,end);
            }
            
        }
    }
    

      递归实现,首先对数组分为左右两组,以基准数为标准,比基准数小的在左边,比基准数大的在右边,然后再进行组内排序

  • 相关阅读:
    RabbitMQ资料
    在网页打开本地程序的思路
    HttpClient的巨坑
    webbrowser设置为相应的IE版本
    cpupower:Shows and sets processor power related values
    golang 国内环境配置
    OSX 创建 randisk(或称 tmpfs)
    Gentoo 搭遗
    ubuntu 去除开机背景
    fabric && cita 调研对比
  • 原文地址:https://www.cnblogs.com/YoungSone/p/14484488.html
Copyright © 2011-2022 走看看