1 var arr = [10, 10, 3, 2, 5 , 4, 8, 3]; 2 3 function reSort(arr) { 4 var temp = 0; 5 var len = arr.length; 6 for(var i =0; i < len - 1; i++) { 7 for(var j = i + 1; j < len; j++) { 8 if(arr[i] < arr[j]) { 9 temp = arr[i]; 10 arr[i] = arr[j]; 11 arr[j] = temp; 12 } 13 } 14 } 15 return arr; 16 } 17 18 function sort(arr) { 19 var temp = 0; 20 var len = arr.length; 21 for(var i =0; i < len - 1; i++) { 22 for(var j = 0; j < len - i - 1; j++) { 23 if(arr[j] > arr[j + 1]) { 24 temp = arr[j]; 25 arr[j] = arr[j + 1]; 26 arr[j + 1] = temp; 27 } 28 } 29 } 30 return arr; 31 } 32 console.log(reSort(arr)); 33 console.log(sort(arr));
时间: 2024-10-07 09:41:29