javascript实现快排

<script> 
 
    var a = [7,4,5,3,2,1,4,5,6,6,2,21,4,53,12,0,-5,31,535,64,11,1,1,1,1];
    function swap(arr, a, b){
        var temp = arr[a];
        arr[a] = arr[b];
        arr[b] =  temp;
    }
    function quickSort(arr,l,r){
        if(l<r){
            var left = l;
            var right = r;
            var temp = a[l];
            while(left<right){
                while(left<right && arr[right]>=temp){
                    right--;
                } 
                if(left<right){
                    arr[left++] = arr[right];
                }
                while(left<right && arr[left]<=temp){
                    left++;
                } 
                if(left<right){
                    arr[right--] = arr[left];
                }
            }
            arr[left] = temp;

            quickSort(arr,l,left-1);
            quickSort(arr,left+1,r);
        }
    }
    quickSort(a,0,a.length-1);
    console.log(a);

</script>

 

posted on 2016-07-26 23:45  杠子  阅读(305)  评论(0编辑  收藏  举报

导航