JavaScript
function sort() {
var array = [1, 10, 9, 8, 6, 4];
console.log(array);
for (var i = 1; i < array.length; i++) {
for (var j = 0; j < array.length - i; j++) {
if (array[j] < array[j + 1]) {
var temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp
}
}
}
console.log(array);
}
Python
def sort_by_bubble(arr):
for i in range(0, len(arr) - 1):
for j in range(0, len(arr) - 1 - i):
if arr[j] < arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
print(arr)
网友评论