【生成规定数量不重复随机数】
function creatRandom( num ){ var randomLen = num, ranArr = [], thisRan = null, whileOff = false, cnt = 0; //初始化push进去1个 thisRan = Math.floor( Math.random()*randomLen ); ranArr.push(thisRan); for( var i=1; i<randomLen; i++ ){ while( true ){ //在生成1个随机数开始判断之旅 thisRan = Math.floor( Math.random()*randomLen ); //whileOff为终止while的开关,只有当thisRan生成了1个与现有数字都不相同的数字时才会关闭while循环 whileOff = true; for( var k=0; k<ranArr.length; k++ ){ if( thisRan == ranArr[k] ){ whileOff = false; break; } } if( whileOff ){ ranArr.push(thisRan); break; } } } return ranArr; }
【冒泡排序】
function bubbling( arr ){ var arr = arr, arrLen = arr.length, newArr = [], broker = null; var oldDate = new Date(); for( var i=0; i<arrLen; i++ ){ for(var j=0; j<arrLen-1-i; j++){ if( arr[j] > arr[j+1] ){ broker = arr[j]; arr[j] = arr[j+1]; arr[j+1] = broker; } } } var newDate = new Date(); console.log(' '); console.log('冒泡排序法'+ arrLen +'个数字花时:'+ (newDate-oldDate) +'毫秒'); console.log( arr ); }
【鸡尾酒排序】
function cocktail( arr ) { var arr = arr, arrLen = arr.length, newArr = [], broker = null; var oldDate = new Date(); for( var i=0; i<arrLen; i++ ){ for(var j=i; j<arrLen-1-i; j++){ if( arr[j] > arr[j+1] ){ broker = arr[j]; arr[j] = arr[j+1]; arr[j+1] = broker; } } for(var k=arrLen-2-i; k>i; k--){ if( arr[k] < arr[k-1] ){ broker = arr[k]; arr[k] = arr[k-1]; arr[k-1] = broker; } } } var newDate = new Date(); console.log( ' ' ); console.log('鸡尾酒排序法'+ arrLen +'个数字花时:'+ (newDate-oldDate) +'毫秒'); console.log( arr ); }
【选择排序】
function selection( arr ){
var arr = arr,
arrLen = arr.length,
newArr = [],
broker = null;
var cnt = 0,
a = cnt;
var oldDate = new Date();
while( cnt < arrLen ){
for(var i=cnt; i<arrLen; i++){
if( arr[i] < arr[a] ){
a = i;
}
}
broker = arr[cnt];
arr[cnt] = arr[a];
arr[a] = broker;
cnt++;
a = cnt;
}
var newDate = new Date();
console.log( ' ' );
console.log('选择排序法'+ arrLen +'个数字花时:'+ (newDate-oldDate) +'毫秒');
console.log( arr );
}
【插入排序】
function insertion( arr ){ var arr = arr, arrLen = arr.length, newArr = [], broker = null; var cnt = 0; var oldDate = new Date(); for(var i=0; i<arrLen-1; i++){ cnt = i; var get = arr[cnt+1]; while( cnt >= 0 && arr[cnt] > get ){ arr[cnt+1] = arr[cnt]; cnt--; } arr[cnt+1] = get; } var newDate = new Date(); console.log( ' ' ); console.log('插入排序法'+ arrLen +'个数字花时:'+ (newDate-oldDate) +'毫秒'); console.log( arr ); }
【未完工的二分插入排序】
//未完工 function binaryInsertion( arr ){ var arr = arr, arrLen = arr.length, newArr = [], broker = null; var oldDate = new Date(); for(var i=0; i<arrLen-1; i++){ var cnt = i; var get = arr[cnt+1]; var tIndex = 0; if( cnt >= 2000 ){ console.log( a ); var j = 1/2; var a = Math.floor( (cnt+1)*j ); var oldA = 0; while( true ){ var dif = get - arr[a]; if( dif == 0 || dif == 1 ){ while( get == arr[a] ){ a++; } tIndex = a; break; } if( dif == -1 && a>0 ){ a--; tIndex = a; break; } if( Math.abs( oldA - a ) <= 10 ){ var l = oldA<a ? oldA : a; for(var k=l,kLen=Math.abs( oldA - a ); k<kLen-1; k++){ if( arr[k] == get ){ tIndex = k; break; } } } //向右 if( dif > 1 ){ oldA = a; j /= 2; a = a + Math.floor( (cnt+1)*j ); } if( dif < -1 ){ oldA = a; j /= 2; a = a - Math.floor( (cnt+1)*j ); } } } while( cnt >= tIndex && arr[cnt] > get ){ arr[cnt+1] = arr[cnt]; cnt--; } arr[cnt+1] = get; } var newDate = new Date(); console.log( ' ' ); console.log('二分插入排序法'+ arrLen +'个数字花时:'+ (newDate-oldDate) +'毫秒'); console.log( arr ); }
备注:
得益于【http://www.cnblogs.com/eniac12/p/5329396.html#s31】而做的这个js版本,做到二分排序实在做不下去了,主要是不知道怎样验证,其次心力憔悴,先放放,以后学习算法时再回来看看。