function bubble_sort (array, length) { var i, j; for(i from 0 to length-1){ for(j from 0 to length-2-i){ if (array[j] > array[j+1]) swap(array[j], array[j+1]) } } }
#include<iostream> usingnamespace std; template<typename T> //整数或浮点数皆可使用,若要使用类(class)或结构体(struct)时必须重载大于(>)运算符 voidbubble_sort(T arr[], int len){ int i, j; for (i = 0; i < len - 1; i++) for (j = 0; j < len - 1 - i; j++) if (arr[j] > arr[j + 1]) swap(arr[j], arr[j + 1]); } intmain(){ int arr[] = { 61, 17, 29, 22, 34, 60, 72, 21, 50, 1, 62 }; int len = (int) sizeof(arr) / sizeof(*arr); bubble_sort(arr, len); for (int i = 0; i < len; i++) cout << arr[i] << ' '; cout << endl; float arrf[] = { 17.5, 19.1, 0.6, 1.9, 10.5, 12.4, 3.8, 19.7, 1.5, 25.4, 28.6, 4.4, 23.8, 5.4 }; len = (float) sizeof(arrf) / sizeof(*arrf); bubble_sort(arrf, len); for (int i = 0; i < len; i++) cout << arrf[i] << ' '<<endl; return0; }
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
privateint[] BubbleSort(int[] array) { var temp = 0;
for (int i = 0; i < array.Length; i++) { for (int j = 0; j < array.Length - 1 - i; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } }
return array; }
java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
privateint[] bubbleSort(int[] array) { int temp; for (inti=0; i < array.length - 1; i++) { booleanFlag=false; // 是否发生交换。没有交换,提前跳出外层循环 for (intj=0; j < array.length - 1 - i; j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; Flag = true; } } if (!Flag) { break; } } return array; }
Ruby
1 2 3 4 5 6 7 8 9 10 11 12
classArray defbubble_sort! for i in0...(size - 1) for j in0...(size - i - 1) self[j], self[j + 1] = self[j + 1], self[j] ifself[j] > self[j + 1] end end self end end
procedurebubble_sort;//排序過程 var i,j:longint; flag:boolean; //flag標誌:若一次排序未發現數據交換,則說明數據已經有序,可以結束排序過程 begin for i:=n-1downto1do begin flag:=true; for j:=1to i do begin if a[j]>a[j+1] then begin swap(j); flag:=false; end; end; if flag thenexit; end; end;
Python
1 2 3 4 5 6 7 8
defbubble_sorted(iterable): new_list = list(iterable) list_len = len(new_list) for i inrange(list_len): for j inrange(list_len - i - 1): if new_list[j] > new_list[j + 1]: new_list[j], new_list[j + 1] = new_list[j + 1], new_list[j] return new_list
For i = 0To UBound(b) - 1 Dim check AsBoolean = False'進入排序後設定一布林變數令其初值為false For j = 0To UBound(b) - 1 - i If b(j) < b(j + 1) Then switch(b(j), b(j + 1))
check = True'進行檢查程序,若符合交換條件即進行兩數值交換(呼叫sub程序) 並於交換後 '將check的值變更為true(表示有進行交換動作,則此數列尚未呈現最終排列序), '離開本層for迴圈後再度將check值重設成false count += 1 Next
If check = FalseThenExitFor'檢查進入迴圈後是否進行過數值交換,若check值為false, '則表示排序進行到此時所有數列的值已呈現期望中的順序, '因此尚未進行完的排序檢查動作可提早結束以提升效率。 Next
MsgBox("共經過了" & count & "次排序")
'泡沫排序由小到大的程式 Dim i, j, count AsInteger Dim check AsBoolean For i = 0To UBound(b) - 1 check=false For j = 0To UBound(b) - 1 - i If b(j) > b(j + 1) Then switch(b(j), b(j + 1)) count += 1 check = True Next If chk = FalseThenExitFor Next MsgBox("共經過了" & count & "次的排序") '兩數值交換程式 PrivateSub switch(ByRef a asinteger, ByRef b asinteger) Dim c AsInteger c = a a = b b = c EndSub
funcbubbleSort(unsortedArray: inout [Int]){ guard unsortedArray.count >1else{ return } for i in0..< unsortedArray.count-1 { var exchanged =false for j in0..< unsortedArray.count-1-i { if unsortedArray[j] > unsortedArray[j+1] { unsortedArray.swapAt(j, j+1) exchanged =true } } if!exchanged { break } } }
// Test var list = [2, 3, 5, 7, 4, 8, 6 ,10 ,1, 9] print(list) bubbleSort(unsortedArray: &list) print(list)
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13
#/bin/bash read -p "Please enter a sequence: " -a num for ((i=0;i<$[${#num[*]}-1];i++));do for((j=0;j<$[$[${#num[*]}-1]-$i];j++));do if [ ${num[$j]} -gt ${num[$[$j+1]]} ];then A=${num[$j]} num[$j]=${num[$[$j+1]]} num[$[$j+1]]=$A
fi done done echo ${num[*]}
IDL
1 2 3 4 5 6 7 8 9 10
FUNCTION BubbleSort, arr FOR i = 0, N_ELEMENTS(arr) - 2 DO BEGIN FOR j = i + 1, N_ELEMENTS(arr) - 1 DO BEGIN IF arr[i] GT arr[j] THEN BEGIN temp = arr[i] & arr[i] = arr[j] & arr[j] = temp ENDIF ENDFOR ENDFOR RETURN, arr END
Julia (编程语言)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
function BubbleSort(A) n = length(A) for i = 1:n for j = 1:(n-i) if(A[j]>A[j+1]) A[j+1],A[j] = A[j],A[j+1] end end end return A end
# Main Code A = [16,586,1,31,354,43,3] print(A) # Original Array print(BubbleSort(A)) # Bubble Sort Array