JS——冒泡排序
方法一:
let arr=[7,12,3,8,1,9,-5,4,22] let i,j,temp; let k=arr.length for (i=1; i<k; i++) { //循环交换的轮数 for(j=0; j< k - i; j++){ //每轮循环的次数 if (arr[j]>arr[j + 1]) { temp=arr[j] arr[j]=arr[j+1] arr[j+1]=temp } } } console.log(arr)
方法二:
let arr=[7,12,3,8,1,9,-5,4,22] console.log(arr.sort(function(a,b) { return a-b }))