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

    package com.fh.algo;
    
    /**
     * 排序算法
     *
     * @author
     * @create 2018-05-26 下午1:00
     **/
    
    import org.junit.Test;
    
    /**
     * 排序算法的学习
     */
    public class Sort {
    
    
        private int[] array = {4,2,5,6,7,3,1};
    
        @Test
        public void quickSort(){
            quickSort(0,array.length-1);
            for (int item:
                 array) {
                System.out.println(item+"
    ");
            }
        }
    
        public void quickSort(int left,int right){
            int i,j,t,temp;
            i=left;
            j=right;
    
            if(left>right){
                return;
            }
    
            temp=array[left];//temp保存基准数
    
    
            while (i!=j){
                //和基数进行比较--小于基数的时候停止
                //从右开始
                while (array[j]>=temp && i<j){
                    j--;
                }
                while (array[i]<=temp && i<j){
                    i++;
                }
                if(i<j){
                    //数据交换
                    t=array[i];
                    array[i]=array[j];
                    array[j]=t;
                }
    
            }
            //基数归位
            array[left]=array[i];
            array[i]=temp;
            quickSort(left,i-1);
            quickSort(i+1,right);
    
        }
    }
    View Code

    快速排序算法实践

    大致思路:

    1、选择基准数据,选择左右指针

    2、从右开始比较选择比基准数小的,从左开始选择比基准数大的数据

    3、对数据进行交换

    4、移动基准数据到相依位置,保证右边的数据大于基准数,左边的小于基准数据

    5、重复第一步

  • 相关阅读:
    86. Partition List
    2. Add Two Numbers
    55. Jump Game
    70. Climbing Stairs
    53. Maximum Subarray
    64. Minimum Path Sum
    122. Best Time to Buy and Sell Stock II
    以场景为中心的产品设计方法
    那些产品经理犯过最大的错
    Axure教程:如何使用动态面板?动态面板功能详解
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/9092740.html
Copyright © 2011-2022 走看看