zoukankan      html  css  js  c++  java
  • 排序之快速排序(quickSort)

    比如说一个数组

    6,1,2,7,9,4,5,10,8
    int a[]={6,1,2,7,9,4,5,10,8};
    

     快排有三个要素:基准flag、左哨兵i、右哨兵

    为了方便一般以数组的第一位作为flag基准

    记住,每次动都是右哨兵J先动,J从8开始探测,寻找比基准6小的数,J--

    I从6开始探测寻找比基准6大的数,i++,交换

    交换完为止,还是从右哨兵J开始探测,寻找比基准6小的数

    左哨兵i++探测比基准大的数,交换

    交换完为止,还是从右哨兵J开始探测,寻找比基准6小的数,找到了3

    左哨兵i++探测比基准大的数

    两个哨兵相遇了,就不能继续走了

    然后此时将该位置的3和基准交换

    这时候,原序列就被分为两个序列了,左序列为3 1 2 5 4右序列为9 7 10 8

    接下来就需要处理这两个序列,首先都是处理左序列3 1 2 5 4

    基准为3 继续上边的操作 调整过后2  1  3  5  4

    左序列为2 1 调整过后为 1 2 3 5 4 6 9 7 10 8

    右序列调整 1 2 3 4 5 9 7 10 8

    接下来处理右序列 9 7 10 8 同样的方法

    代码如下

     1 public class QuickSort {  
     2       
     3     /** 
     4      * 将数组的某一段元素进行划分,小的在左边,大的在右边 
     5      * @param a 
     6      * @param start 
     7      * @param end 
     8      * @return 
     9      */  
    10     public static int divide(int[] a, int start, int end){  
    11         //每次都以最右边的元素作为基准值  
    12         int base = a[end];  
    13         //start一旦等于end,就说明左右两个指针合并到了同一位置,可以结束此轮循环。  
    14         while(start < end){  
    15             while(start < end && a[start] <= base)  
    16                 //从左边开始遍历,如果比基准值小,就继续向右走  
    17                 start++;  
    18             //上面的while循环结束时,就说明当前的a[start]的值比基准值大,应与基准值进行交换  
    19             if(start < end){  
    20                 //交换  
    21                 int temp = a[start];  
    22                 a[start] = a[end];  
    23                 a[end] = temp;  
    24                 //交换后,此时的那个被调换的值也同时调到了正确的位置(基准值右边),因此右边也要同时向前移动一位  
    25                 end--;  
    26             }     
    27             while(start < end && a[end] >= base)  
    28                 //从右边开始遍历,如果比基准值大,就继续向左走  
    29                 end--;  
    30             //上面的while循环结束时,就说明当前的a[end]的值比基准值小,应与基准值进行交换  
    31             if(start < end){  
    32                 //交换  
    33                 int temp = a[start];  
    34                 a[start] = a[end];  
    35                 a[end] = temp;  
    36                 //交换后,此时的那个被调换的值也同时调到了正确的位置(基准值左边),因此左边也要同时向后移动一位  
    37                 start++;  
    38             }     
    39               
    40         }  
    41         //这里返回start或者end皆可,此时的start和end都为基准值所在的位置  
    42         return end;  
    43     }  
    44   
    45     /** 
    46      * 排序 
    47      * @param a 
    48      * @param start 
    49      * @param end 
    50      */  
    51     public static void sort(int[] a, int start, int end){  
    52         if(start > end){  
    53             //如果只有一个元素,就不用再排下去了  
    54             return;  
    55         }   
    56         else{  
    57             //如果不止一个元素,继续划分两边递归排序下去  
    58             int partition = divide(a, start, end);  
    59             sort(a, start, partition-1);  
    60             sort(a, partition+1, end);  
    61         }  
    62               
    63     }  
    64     public static void main(String[] args) {  
    65         
    66         int[] b = new int[]{2,7,4,5,10,1,9,3,8,6};  
    67         int[] c = new int[]{1,2,3,4,5,6,7,8,9,10};  
    68         int[] d = new int[]{10,9,8,7,6,5,4,3,2,1};  
    69         int[] a = new int[]{1,10,2,9,3,2,4,7,5,6};  
    70               
    71         sort(a, 0, a.length-1);  
    72               
    73         System.out.println("排序后的结果:");  
    74         for(int x : a){  
    75             System.out.print(x+" ");  
    76         }  
    77     }  
    78 
    79 } 

      

  • 相关阅读:
    清除 Windows 系统垃圾的 bat
    java通过System.getProperty获取系统属性
    【转】Angular之constructor和ngOnInit差异及适用场景
    p中不能包含div
    How to make PostgreSQL functions atomic?
    How to chain a command after sudo su?
    javascript柯里化
    如何在Angular优雅编写HTTP请求
    angular default project (angular.json的解读)
    error:crosses initialization of ...的解决办法
  • 原文地址:https://www.cnblogs.com/Darius-Bennett/p/7840474.html
Copyright © 2011-2022 走看看