Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

快速排序在JS下的实现 (转自个人百度空间) #17

Open
mishe opened this issue Dec 2, 2015 · 0 comments
Open

快速排序在JS下的实现 (转自个人百度空间) #17

mishe opened this issue Dec 2, 2015 · 0 comments

Comments

@mishe
Copy link
Owner

mishe commented Dec 2, 2015

function ksort(arr){

        var a=arr.slice(),len=a.length,start=0,end=len-1;

        function sort(start,end){

            if(start>end)
                return;
            var tmp=a[start],i=start,j=end,t;
            while(i!=j){
                while(a[j]>=tmp && i<j){
                    j--
                }
                while(a[i]<=tmp && i<j){
                    i++
                }
                if(i<j){
                    t=a[i];
                    a[i]=a[j];
                    a[j]=t;
                }

            }

            a[start]=a[i];
            a[i]=tmp;
            sort(start,i-1)
            sort(i+1,end)
        }
        sort(start,end)
        return a;
    }

d=[1,4,3,9,0,2,5,7,8,21,44,43];

    ksort(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant