JAVA語言
1 public static void bubbleSort(int []arr) { 2 for(int i =0;i<arr.length-1;i++) { 3 for(int j=0;j<arr.length-i-1;j++) {//-1為了防止溢出 4 if(arr[j]>arr[j+1]) { 5 int temp = arr[j]; 6 7 arr[j]=arr[j+1]; 8 9 arr[j+1]=temp; 10 } 11 } 12 } 13 }
Visual Fox Pro語言
?'Original Array ' + CHR(43147)
DIMENSION arr(10)
FOR i = 1 TO 10
arr(i) = ROUND(rand()*100,0)
ENDFOR
DISPLAY MEMORY LIKE arr
?'After Sort ' + CHR(43147)
FOR i = 1 TO 10
FOR j = i + 1 TO 10
IF arr(i) > arr(j)
lnTemp = arr(i)
arr(i) = arr(j)
arr(j) = lnTemp
ENDIF
ENDFOR
ENDFOR
DISPLAY MEMORY LIKE arr
Python3
1 def bubble_sort(nums): 2 for i in range(len(nums) - 1): # 這個循環負責設置冒泡排序進行的次數 3 for j in range(len(nums) - i - 1): # j為列表下標 4 if nums[j] > nums[j + 1]: 5 nums[j], nums[j + 1] = nums[j + 1], nums[j] 6 return nums 7 print(bubble_sort([45, 32, 8, 33, 12, 22, 19, 97])) 8 # 輸出:[8, 12, 19, 22, 32, 33, 45, 97]
Swift
1 func bubbleSort(_ nums: inout [Int]) { 2 let n = nums.count 3 for i in 0..<n { 4 for j in 0..<(n - 1 - i) { 5 if nums[j] > nums[j + 1] { 6 nums.swapAt(j, j + 1) 7 } 8 } 9 } 10 print(nums) 11 } 12 13 var nums = [1,3,7,8,9] 14 bubbleSort(&nums)
C++排序
1 #include <iostream> 2 using namespace std; 3 template<typename T> 4 //整數或浮點數皆可使用 5 void bubble_sort(T arr[], int len) 6 { 7 int i, j; T temp; 8 for (i = 0; i < len - 1; i++) 9 for (j = 0; j < len - 1 - i; j++) 10 if (arr[j] > arr[j + 1]) 11 { 12 temp = arr[j]; 13 arr[j] = arr[j + 1]; 14 arr[j + 1] = temp; 15 } 16 } 17 int main() 18 { 19 int arr[] = { 61, 17, 29, 22, 34, 60, 72, 21, 50, 1, 62 }; 20 int len = (int) sizeof(arr) / sizeof(*arr); 21 bubble_sort(arr, len); 22 for (int i = 0; i < len; i++) 23 cout << arr[i] << ' '; 24 cout << endl; 25 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 }; 26 len = (int) sizeof(arrf) / sizeof(*arrf); 27 bubble_sort(arrf, len); 28 for (int i = 0; i < len; i++) 29 cout << arrf[i] << ' '; 30 return 0; 31 }
RUBY
1 def bubbleSort(array) 2 return array if array.size < 2 3 (array.size - 2).downto(0) do |i| 4 (0 .. i).each do |j| 5 array[j], array[j + 1] = array[j + 1], array[j] if array[j] >= array[j + 1] 6 end 7 end 8 return array 9 end
PHP
1 function bubbleSort($numbers) { 2 $cnt = count($numbers); 3 for ($i = 0; $i < $cnt; $i++) { 4 for ($j = 0; $j < $cnt - $i - 1; $j++) { 5 if ($numbers[$j] > $numbers[$j + 1]) { 6 $temp = $numbers[$j]; 7 $numbers[$j] = $numbers[$j + 1]; 8 $numbers[$j + 1] = $temp; 9 } 10 } 11 } 12 13 return $numbers; 14 } 15 16 $num = array(20, 40, 60, 80, 30, 70, 90, 10, 50, 0); 17 var_dump(bubbleSort($num)); 18 19 //輸出結果如下: 20 //array(10) { 21 // [0]=> 22 // int(0) 23 // [1]=> 24 // int(10) 25 // [2]=> 26 // int(20) 27 // [3]=> 28 // int(30) 29 // [4]=> 30 // int(40) 31 // [5]=> 32 // int(50) 33 // [6]=> 34 // int(60) 35 // [7]=> 36 // int(70) 37 // [8]=> 38 // int(80) 39 // [9]=> 40 // int(90) 41 //}
C#語言
1 namespace 數組排序 2 { 3 class Program 4 { 5 static void Main(string[] args) 6 { 7 int temp = 0; 8 int[] arr = {23, 44, 66, 76, 98, 11, 3, 9, 7}; 9 #region該段與排序無關 10 Console.WriteLine("排序前的數組:"); 11 foreach (int item in arr) 12 { 13 Console.Write(item + ""); 14 } 15 Console.WriteLine(); 16 #endregion 17 for (int i = 0; i < arr.Length - 1; i++) 18 { 19 #region將大的數字移到數組的arr.Length-1-i 20 for (int j = 0; j < arr.Length - 1 - i; j++) 21 { 22 if (arr[j] > arr[j + 1]) 23 { 24 temp = arr[j + 1]; 25 arr[j + 1] = arr[j]; 26 arr[j] = temp; 27 } 28 } 29 #endregion 30 } 31 Console.WriteLine("排序后的數組:"); 32 foreach (int item in arr) 33 { 34 Console.Write(item+""); 35 } 36 Console.WriteLine(); 37 Console.ReadKey(); 38 } 39 } 40 }
Erlang
1 bubble_sort(L)-> 2 bubble_sort(L,length(L)). 3 4 bubble_sort(L,0)-> 5 L; 6 bubble_sort(L,N)-> 7 bubble_sort(do_bubble_sort(L),N-1). 8 9 do_bubble_sort([A])-> 10 [A]; 11 do_bubble_sort([A,B|R])-> 12 caseA<Bof 13 true->[A|do_bubble_sort([B|R])]; 14 false->[B|do_bubble_sort([A|R])] 15 end.
C語言
1 #include <stdio.h> 2 #define SIZE 8 3 4 void bubble_sort(int a[], int n); 5 6 void bubble_sort(int a[], int n) 7 { 8 int i, j, temp; 9 for (j = 0; j < n - 1; j++) 10 for (i = 0; i < n - 1 - j; i++) 11 { 12 if(a[i] > a[i + 1]) 13 { 14 temp = a[i]; 15 a[i] = a[i + 1]; 16 a[i + 1] = temp; 17 } 18 } 19 } 20 21 int main() 22 { 23 int number[SIZE] = {95, 45, 15, 78, 84, 51, 24, 12}; 24 int i; 25 bubble_sort(number, SIZE); 26 for (i = 0; i < SIZE; i++) 27 { 28 printf("%d\n", number[i]); 29 } 30 } 31 JavaScript 32 function bubbleSort(arr) { 33 var i = arr.length, j; 34 var tempExchangVal; 35 while (i > 0) { 36 for (j = 0; j < i - 1; j++) { 37 if (arr[j] > arr[j + 1]) { 38 tempExchangVal = arr[j]; 39 arr[j] = arr[j + 1]; 40 arr[j + 1] = tempExchangVal; 41 } 42 } 43 i--; 44 } 45 return arr; 46 } 47 48 var arr = [3, 2, 4, 9, 1, 5, 7, 6, 8]; 49 var arrSorted = bubbleSort(arr); 50 console.log(arrSorted); 51 alert(arrSorted);
控制台將輸出:[1, 2, 3, 4, 5, 6, 7, 8, 9]
並且彈窗;
Visual Basic語言
1 Sub maopao() 2 Dim a = Array(233, 10086, 31, 15, 213, 5201314, 427) 3 Dim i As Integer, j As Integer 4 5 For i = UBound(a) - 1 To 0 Step -1 6 For j = 0 To i 7 If a(j) > a(j + 1) Then 8 a(j) = a(j) + a(j + 1) 9 a(j + 1) = a(j) - a(j + 1) 10 a(j) = a(j) - a(j + 1) 11 End If 12 Next j 13 Next i 14 For i = 0 To UBound(a) 15 Print a(i) 16 Next i 17 End Sub
Objective-C
1 for (int i = 0; i<result.count; i++) { 2 for (int j = 0; j<result.count-1-i; j++) { 3 NSInteger left = [result[j] integerValue]; 4 NSInteger right = [result[j+1] integerValue]; 5 if (left<right) { 6 [result exchangeObjectAtIndex:j withObjectAtIndex:j+1]; 7 } 8 } 9 } 10 NSLog(@"%@",result);
Go語言
package main import ( "fmt" ) const ( LENGTH = 8 ) func main() { var tmp int number := []int{95, 45, 15, 78, 84, 51, 24, 12} for i := 0; i < LENGTH; i++ { for j := LENGTH - 1; j > i; j-- { if number[j] < number[j-1] { tmp = number[j-1] number[j-1] = number[j] number[j] = tmp } } } for i := 0; i < LENGTH; i++ { fmt.Printf("%d ", number[i]) } fmt.Printf("\n") }
GO語言2
func BubbleSort(values []int) { flag := true vLen := len(values) for i := 0; i < vLen-1; i++ { flag = true for j := 0; j < vLen-i-1; j++ { if values[j] > values[j+1] { values[j], values[j+1] = values[j+1], values[j] flag = false continue } } if flag { break } } }
PASCAL
1 var 2 a:array[1..4] of integer; 3 i, j, temp, n:integer; 4 begin 5 read(n); 6 for i := 1 to n do read(a[i]); 7 for i := 1 to n do 8 for j := 1 to n-i do 9 if a[j] > a[j + 1] then 10 begin 11 temp := a[j]; 12 a[j] := a[j + 1]; 13 a[j+1] := temp; 14 end; 15 for i:= 1 to n do write(a[i]); 16 end.
Python
1 def bubble(bubbleList): 2 listLength = len(bubbleList) 3 while listLength > 0: 4 for i in range(listLength - 1): 5 if bubbleList[i] > bubbleList[i+1]: 6 bubbleList[i] = bubbleList[i] + bubbleList[i+1] 7 bubbleList[i+1] = bubbleList[i] - bubbleList[i+1] 8 bubbleList[i] = bubbleList[i] - bubbleList[i+1] 9 listLength -= 1 10 print bubbleList 11 if __name__ == '__main__': 12 bubbleList = [3, 4, 1, 2, 5, 8, 0] 13 bubble(bubbleList) 14 上面方法過於難懂,附上簡單for循環冒泡排序 15 L = [3, 5, 6, 7, 8, 1, 2] 16 for i in range(len(L)-1): 17 for j in range(len(L)-1-i): 18 if L[j] >L[j+1]: 19 L[j], L[j+1] = L[j+1], L[j] 20 print(L)
輸出即可檢驗結果
匯編語言
1 DATAS SEGMENT 2 A DW 9,4,26,85,38 3 DATAS ENDS 4 CODES SEGMENT 5 ASSUME CS:CODES,DS:DATAS 6 START: 7 MOV AX,DATAS 8 MOV DS,AX 9 MOV DI,4;初始化外循環次數為數組個數-1 10 LP1:MOV CX,DI;外循環次數初值為數組個數-1 11 MOV BX,0;基址初值BX為0 12 LP2:MOV AX,A[BX] 13 CMP AX,A[BX+2] 14 JGE CONT;大於等於不交換 15 XCHG AX,A[BX+2];小於交換,AX保存的為較大的數 16 MOV A[BX],AX;A[BX]保存的為較大的數,准備進行下一次比較, 17 CONT:ADD BX,2;基址初值BX+2,字變量,下一個字偏移地址+2 18 LOOP LP2 ;內循環次數-1,內循環次數是否為0? 19 DEC DI;外循環次數-1 20 JNZ LP1;外循環次數是否為0? 21 MOV AH,4CH 22 INT 21H 23 CODES ENDS 24 END START