1.冒泡排序 从小到大
function bubbing(){
var oldList = [1,6,32,7,8,245,2345,25,4,245245,14312];
for(var i = 0; i < oldList.length - 1; i++)
{
for(var j = 0; j < oldList.length - i - 1; j++)
{
if(oldList[j] > oldList[j+1])
{
var temp = oldList[j];
oldList[j] = oldList[j+1];
oldList[j+1] = temp;
}
}
}
return oldList;
}
console.log(bubbing());
2.插入排序 从小到大
function insertSort(oldList){
//var oldList = [5,3,8,4,5,5,5,2,6];
var newList = [];
//alert(oldList.length);
for(var i = 0; i < oldList.length; i++)
{
var len = newList.length;
if(i == 0)
{
newList[0] = oldList[0];
continue;
}
for(var j = len - 1; j >= 0; j--)
{
if(oldList[i] <= newList[j])
{
newList[j+1] = newList[j];
newList[j] = oldList[i];
}
else
{
newList[j+1] = oldList[i];
break;
}
}
}
return newList;
}
3.选择排序 从小到大
function select1(){ //oldList newList
var oldList = [1,6,32,7,8,245,2345,25,4,245245,14312];
for(var i = 0; i < oldList.length; i++)
{
for(var j = oldList.length - 1; j > i; j--)
{
if(oldList[i] >= oldList[j])
{
var temp = oldList[i];
oldList[i] = oldList[j];
oldList[j] = temp;
}
}
}
return oldList;
}
//console.log(bubbing());
function select2(){
var oldList = [1,6,32,7,8,245,2345,25,4,245245,14312];
for(var n = 0; n < oldList.length; n++)
{
for(var i = n + 1; i < oldList.length ; i++)
{
if(oldList[n] >= oldList[i])
{
var temp = oldList[n];
oldList[n] = oldList[i];
oldList[i] = temp;
}
}
}
return oldList;
}
//console.log(select());