Visual Basic实现:
Private Sub Command1_Click()
Dim bubbleList(4) As Integer
bubbleList(0) = 1
bubbleList(1) = 11
bubbleList(2) = 132
bubbleList(3) = 21
bubbleList(4) = 43
length = UBound(bubbleList)
For i = length - 1 To 0 Step -1
For j = 0 To i Step 1
If bubbleList(j) > bubbleList(j + 1) Then
temp = bubbleList(j)
bubbleList(j) = bubbleList(j + 1)
bubbleList(j + 1) = temp
End If
Next
Next
For k = 0 To length
Print (bubbleList(k))
Next
End Sub
C实现:
#include <stdio.h>
main(){
int bubbleList[5] = {12, 1, 32, 11, 15};
int length = sizeof(bubbleList) / sizeof(int);
for (int i = length - 1; i > 0; i--){
for (int j = 0; j < i; j++){
if (bubbleList[j] > bubbleList[j + 1]){
int temp = bubbleList[j];
bubbleList[j] = bubbleList[j + 1];
bubbleList[j + 1] = temp;
}
}
}
for (int k = 0; k < length; k++){
printf("%d\n", bubbleList[k]);
}
return 1;
}
Java实现:
//bubblo algorithm
public static int [] bubbloAlgorithm(int [] array) {
int length = array.length;
for (int i = length-1; i > 1; i--) {
for (int j = 0; j < i; j++) {
if (array[j] > array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
return array;
}
Python3实现:
bubbleList = [1, 32, 11, 31, 33]
length = len(bubbleList) - 1
for i in range(length, -1, -1):
for j in range(0, i, 1):
if bubbleList[j] > bubbleList[j + 1]:
temp = bubbleList[j];
bubbleList[j] = bubbleList[j + 1]
bubbleList[j + 1] = temp
for v in bubbleList:
print(v)
JavaScript实现:
function bubble() {
var list = new Array();
list.push(1);
list.push(31);
list.push(11);
list.push(21);
list.push(15);
var length = list.length;
for (var i = length - 1; i > 0; i--) {
for (var j = 0; j < i; j++) {
if (list[j] > list[j + 1]) {
var temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}