1.冒泡排序;
function bubbleSort(array) {
var length = array.length;
for(var i = length - 1; i > 0; i--) {
for(var j = 0; j < i; j++) {
if(array[j] > array[j + 1]) {
var temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
2.去重
function remove(array){
var newArr = [];
for(var i=0;i<array.length;i++){
if(newArr.indexOf(array[i])==-1){
newArr.push(array[i]);
}
}
return newArr;
}
3.去重和排序的组合
function bubbleSort(array) {
var newArr = [];
var length = array.length;
for(var i = length - 1; i > 0; i--) {
for(var j = 0; j < i; j++) {
if(array[j] > array[j + 1]) {
var temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
if(newArr.indexOf(array[i]) == -1) {
newArr.unshift(array[i]);
}
}
return newArr;
}
时间: 2024-10-12 00:32:49