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快排 #6

Open
JesseZhao1990 opened this issue Jun 4, 2018 · 2 comments
Open

js快排 #6

JesseZhao1990 opened this issue Jun 4, 2018 · 2 comments
Labels

Comments

@JesseZhao1990
Copy link
Owner

var quickSort = function(arr) {
  if(arr.length<=1) {return arr};
  var pivotIndex = Math.floor(arr.length / 2);
  var pivot = arr.splice(pivotIndex,1)[0];
  var left = [];
  var right = [];

  for(var i=0;i<arr.length;i++){
    if(arr[i]<pivot){
      left.push(arr[i]);
    }else{
      right.push(arr[i]);
    }
  }

  return quickSort(left).concat([pivot], quickSort(right));

}
@qingzhu1224
Copy link

前段时间在知乎上,看到有人在分析这一版。

@JesseZhao1990
Copy link
Owner Author

@qingzhu1224 是的,很多人都说阮老师这样的快排是错的。其实我不大赞成。。。

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

No branches or pull requests

2 participants