Java实现快速排序

package Sort;

import java.util.Arrays;

public class QuickSort {

    public static void quicksort(int[] list, int left, int right) {
        
        if(left > right){
            return;
        }
        
        int pivot = list[left];
        int i = left;
        int j = right;
        int tmp;
        
        while(i != j){
            
            while(pivot <= list[j] && i < j){
                j--;
            }
            while(pivot >= list[i] && i < j){
                i++;
            }
            
            if(i < j) {
                tmp = list[j];
                list[j] = list[i];
                list[i] = tmp;
            }
        }
        
        list[left] = list[i];
        list[i] = pivot;
        
        quicksort(list, left, i - 1);
        quicksort(list, i + 1, right);
        
    }
    
    
    public static void main(String[] args) {
        int[] list = {5,4,3,2,1,6,8,7};
        quicksort(list,0,list.length - 1);
        System.out.println(Arrays.toString(list));
    }

}

 

posted @ 2016-05-11 15:58  finalboss1987  阅读(112)  评论(0编辑  收藏  举报