在51CTO学院看到的教学视频,保存下来,说不定有用到的时候
冒泡排序
For(i=0; i<Arr.length; i++; ){
For(j=i+1; j<Arr.length; j++){
If(Arr[0] > Arr[i]){
Var temp = Arr[0];
Arr[0] = Arr[i];
Arr[i] = temp;
}
}
}
二.插入排序
Var Arr1=[2,8,0,34..]
Var Arr2[];
Arr2[0] = Arr1[0];
Var flag =0;
For(var i=o; i<arr1.length; i++){
Flag = 0;
For(j=0; j<Arr2.length; j++){
If(Arr1[i] < Arr2[j]){
Arr2.splice(j, 0 ,Arr1[i]);
Flag=1;
Berak;
}
}
If(flag==0){
Arr2.push(Arr1[i]);
}
}
三.二分排序(适合大数据量的排序)
Var Arr1=[2,8,0,34..];
Var Arr2 = [];
Arra[0] = Arr1[0];
Var left = 0;
Var right = 0;
Var point = 0;
For(var i=o; i<arr1.length; i++){
Left=0;
Right=Arr2.length;
For(j=0; j<Arr2.length; j++){
Point = Math.floor((left+right)/2);
If(Arr2[point] < Arr1[i]){
Left =point+1;
}else{
Right = point;
}
If(left==right){
Arr2.splice(left, 0, Arr1[i]);
Break;
}
}
}