Visual Basic实现:
Private Sub Command2_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 = 0 To length - 1
minIndex = i
For inn = i + 1 To length
If bubbleList(inn) < bubbleList(minIndex) Then
minIndex = inn
End If
Next
temp = bubbleList(i)
bubbleList(i) = bubbleList(minIndex)
bubbleList(minIndex) = temp
Next
For k = 0 To length
Print (bubbleList(k))
Next
End Sub
C实现:
#include <stdio.h>
main(){
int a[5] = {11,13,1,31,14};
int length = sizeof(a) / sizeof(int);
for (int i = 0; i < length - 1; i++){
int min = i;
for(int j = i + 1; j < length; j++){
if(a[min] > a[j]){
min = j;
}
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
for (int k = 0; k < length; k++){
printf("%d\n", a[k]);
}
return 1;
}
Java实现:
//chooseAlgorithm
public static int [] chooseAlgorithm (int [] array) {
int out,min,in;
for (out = 0; out < array.length - 1; out++) {
min = out;
for (in = out + 1; in< array.length; in++) {
if (array[in] < array[min]) {
min = in;
}
}
int temp = array[out];
array[out] = array[min];
array[min] = temp;
}
return array;
}
Python3实现:
bubbleList = [1, 32, 11, 31, 33]
length = len(bubbleList) - 1
for i in range(0, length, 1):
minIndex = i
for j in range(i + 1, length + 1, 1):
if bubbleList[minIndex] > bubbleList[j]:
minIndex = j
temp = bubbleList[i]
bubbleList[i] = bubbleList[minIndex]
bubbleList[minIndex] = temp
for v in bubbleList:
print(v)
JavaScript实现:
function choose() {
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 = 0; i < length - 1; i++){
minIndex = i;
for (var j = i + 1; j < length; j++){
if (list[minIndex] > list[j]){
minIndex = j;
}
}
var temp = list[i];
list[i] = list[minIndex];
list[minIndex] = temp;
}
alert(list)
}