zoukankan      html  css  js  c++  java
  • Quick Sort

    character:as the name implies,this sort method is usaully quicker than other ones.

    the key part of this sort algorithm is the partition of head and bottom.

    as to the realization ,I 'll show two method.one is simple,and the other is a little complex.the complexity is reference to the thinking.

    the simple one:

    public static int partition(Comparable [] a,int lo,int hi){
    int index=lo;
    Comparable temp=a[lo];
    for(int i=lo+1;i<=hi;++i){
    if(less(a[i],temp)){
    a[index]=a[i];
    index=i;
    }
    }
    a[index]=temp;
    return index;
    }
    there is a problem in this realization:it doesnot consider the negtive direction.
    so here is a substitute,a little redundency,I'll reconstruct it later:
        public static int partition(Comparable [] a,int lo,int hi){
    int len=a.length;
    boolean loInclude=(lo>=0) && (lo<len);
    boolean hiInclude=(hi>=0) && (hi<len);
    assert (!loInclude) || (!hiInclude);
    assert (hi<lo);

    Comparable temp=a[lo];
    int i=lo;
    int j=hi;

    while(true) {
    while ((!lessOrEqual(a[j], temp)) && (i<j)) {
    --j;
    if (j == lo) break;
    }
    a[i] = a[j];
    if (i == hi) break;if (j == lo) break;if(i>=j) break;
    ++i;

    while (lessOrEqual(a[i], temp) && (i<j)) {
    ++i;
    if (i == hi) break;
    }
    a[j] = a[i];
    if (i == hi) break;if (j == lo) break;if(i>=j) break;
    --j;
    if(i>=j) break;
    }
    a[i]=temp;
    return i;
    }

    the complex one:

    public static int updatePartition(Comparable[]a,int lo,int hi){
    if(hi<=lo) return lo;
    Comparable v=a[lo];
    int head=lo;
    int tail=hi+1;
    while(true){
    while(less(a[++head],v)){if(head==hi) break;}
    while(less(v,a[--tail])){if(tail==lo) break;}
    if(head>=tail) break;
    swap(a,head,tail);
    }
    swap(a,lo,tail);
    return tail;
    }

    we now can cut an array into two part by the partition fuction,the head part is less than the middle one while the tail part is greater than the middle one.

    so we can sort the total array by recursion.you can program as follow:

    private static void quickSort(Comparable[]a,int lo,int hi){
    if(hi<=lo) return;
    //int mid=partition(a,lo,hi);
    int mid=updatePartition(a,lo,hi);
    quickSort(a,lo,mid-1);
    quickSort(a,mid+1,hi);
    }

    In additon ,the customer may be reluctant to input any redundancy infomation.so we can pack the sort function into a new function with less input parameters,and use it as an interface to the customer:

    public static void quickSort(Comparable[] a){
    quickSort(a,0,a.length-1);
    }

    The end.

  • 相关阅读:
    Note/Solution 转置原理 & 多点求值
    Note/Solution 「洛谷 P5158」「模板」多项式快速插值
    Solution 「CTS 2019」「洛谷 P5404」氪金手游
    Solution 「CEOI 2017」「洛谷 P4654」Mousetrap
    Solution Set Border Theory
    Solution Set Stirling 数相关杂题
    Solution 「CEOI 2006」「洛谷 P5974」ANTENNA
    Solution 「ZJOI 2013」「洛谷 P3337」防守战线
    Solution 「CF 923E」Perpetual Subtraction
    KVM虚拟化
  • 原文地址:https://www.cnblogs.com/ssMellon/p/6543892.html
Copyright © 2011-2022 走看看