//快速排序
functionquickSort(elements) {
if(elements.length<=1) {
returnelements;
}
varpivotIndex=Math.floor(elements.length/2);
varpivot=elements.splice(pivotIndex,1)[0];
varleft=[];
varright=[];
for(vari=0;i
if(elements[i]
left.push(elements[i]);
}else{
right.push(elements[i]);
}
}
returnquickSort(left).concat([pivot],quickSort(right));
}
//var elements = [5,6,2,1,3,8,7,1.3,5.5];
// console.log(quickSort(elements));
//冒泡排序
functionsort(elements) {
for(vari=0;i
for(varj=0;j
if(elements[j]>elements[j+1]) {
varswap=elements[j];
elements[j]=elements[j+1];
elements[j+1]=swap;
}
}
}
}
// var elements_2 = [16,3,1,5,7,2,4,9,6,10,8];
// sort(elements_2);
//选择排序
functionchooseSort(elements) {
for(vari=1;i
if(elements[i]
varguard=elements[i];
varj=i-1;
elements[i]=elements[j];
while(j>=0&&guard
elements[j+1]=elements[j];
j--;
}
elements[j+1]=guard;
}
}
}
varelements=[5,1,4,16,7,3,8];
chooseSort(elements);