排序
//冒泡排序
/*function num(arr){
var a = 0;
for(var i=0;i<arr.length-1;i++){
for(var j=0;j<arr.length-i;j++){
if(arr[j]>arr[j+1]){
a = arr[j+1];
arr[j+1] = arr[j];
arr[j] = a;
}
}
}
return arr;
}
var arr = [2,5,5,1,7,8];
console.log(num(arr));*/
//选择排序
/* function num(arr){
var a;
for(var i=0;i<arr.length-1;i++){
for(var j=1+i ;j<arr.length;j++){
if(arr[i]>arr[j]){
a = arr[j];
arr[j] = arr[i];
arr[i] = a;
}
}
}
return arr;
}
var arr = [2,5,5,1,7,8];
console.log(num(arr));*/
//快速排序
function quick(arr){
if(arr.length <= 1){
return arr;
}
var left = [];
var right = [];
var center;
center = arr.length % 2 == 0?arr.length/2:(arr.length-1)/2;
var mid = arr[center];
for (var i = 0; i <arr.length; i++) {
if(i != center && arr[i] <= mid){
left.push(arr[i]);
}
if(i != center && arr[i] > mid){
right.push(arr[i]);
}
}
return quick(left).concat(mid).concat(quick(right));
}
var arr = [10,9,22,71,56,33,91,56,32];
console.log(quick(arr))