JavaScript 题目

1.

var a=[0],b=a; 
b[0]=2;
console.log(a+b); //22

a=[0], b=a, b=[1];
console.log(a+b); //01

2.快速排序法

var quickSort = function(arr) {
  if (arr.length <= 1) { return arr; }
  var pivotIndex = Math.floor(arr.length / 2);
    arr=arr.concat();
  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));
};

测试:

var arr=[1,8,7,6,5,4,3,2];
var list=quickSort(arr);
console.log(arr);
console.log(list);

 

posted @ 2013-10-23 10:49  hongdada  阅读(179)  评论(0编辑  收藏  举报