冒泡排序:
var arr = [9, 8, 7, 6, 2, 1, 3, 5, 4]
for(n = 0; n < arr.length; n++) {
for(var m = n + 1; m < arr.length; m++){
if(arr[n] > arr[m]) {
var temp = arr[n]; arr[n] = arr[m]; arr[m] = temp
}
}
}
document.write(arr)
去重:
var arr1=[1,1,2,2,3,3,4,4,5,5]
var arr2=[ ]
for(var m=0; m<arr1.length; m++){
var a=0 for(var n=0; n<arr2.length; n++){
if(arr1[m]==arr2[n])
{ a=1 }
}
if(a==0){
arr2.push(arr1[m]) }
}
alert(arr2)
时间: 2024-11-06 07:25:54