快速排序法 java实现


**
 * @author: zsg
 * @description: 快速排序法 
 * @date: 2018/6/8 15:25
 * @modified:
 */
public class demo6 {

    public static void main(String[] args) {
        int[] arr = {2,4,1,2,3,5,4,5,8,1,3,6,5,2,7,9,7};
        Qsort(arr, 0, arr.length - 1);

        for (int i = 0; i <arr.length ; i++) {
            System.out.println(arr[i]);
        }

    }

    static  int Partition(int A[],int low ,int height){
       int flag =  A[low];//将选取的枢轴暂时存起来
       int pivot = A[low];//选取的枢轴
       while (low<height){
           //从右--》左 和选取的枢轴比较
           while (low<height&&A[height]>=pivot){
               --height;
           }
           //如果条件成立就把low的换成height
           if (low<height){
               A[low++] = A[height];
           }
           //与上述相同论述
           while (low<height&&A[low]<=pivot){
               ++low;
           }
           if (low<height){
               A[height--] = A[low];
           }
       }
       //选取的枢轴放到正确的位置
       A[low] = flag;
       return  low;
    }

    static  void  Qsort(int A[],int low ,int height){
        if (low<height){
            int partition = Partition(A, low, height);
            Qsort(A,low,partition-1);//左边
            Qsort(A,partition+1,height);//右边
        }
    }
}
 
 

加入群聊  602640939

阿里云服务器交流群

--------------------------------------------------------------------------------------------------------


猜你喜欢

转载自blog.csdn.net/weixin_42102841/article/details/80632262