# 各编程语言冒泡排序的实现

JAVA语言

1publicstaticvoidbubbleSort(int[]arr) {

2for(inti =0;i

3for(intj=0;j

4if(arr[j]>arr[j+1]) {

5inttemp = arr[j];

6

7arr[j]=arr[j+1];

8

9arr[j+1]=temp;

10}

11}

12}

13}

Python

1defbubble(bubbleList):

2listLength = len(bubbleList)

3whilelistLength > 0:

4foriinrange(listLength - 1):

5ifbubbleList[i] > bubbleList[i+1]:

6bubbleList[i] =bubbleList[i] + bubbleList[i+1]

7bubbleList[i+1] =bubbleList[i] - bubbleList[i+1]

8bubbleList[i] =bubbleList[i] - bubbleList[i+1]

9listLength -= 1

10printbubbleList

11if__name__=='__main__':

12bubbleList = [3, 4, 1, 2, 5, 8, 0]

13bubble(bubbleList)

14上面方法过于难懂，附上简单for循环冒泡排序

15L = [3, 5, 6, 7, 8, 1, 2]

16foriinrange(len(L)-1):

17forjinrange(len(L)-1-i):

18ifL[j] >L[j+1]:

19L[j], L[j+1] = L[j+1], L[j]

20print(L)

Visual Fox Pro语言

?'Original Array '+ CHR(43147)

DIMENSION arr(10)

FOR i = 1 TO 10

arr(i) = ROUND(rand()*100,0)

ENDFOR

DISPLAY MEMORYLIKE 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 MEMORYLIKE arr

Python3

1defbubble_sort(nums):

2foriinrange(len(nums) - 1):#这个循环负责设置冒泡排序进行的次数

3forjinrange(len(nums) - i - 1):# j为列表下标

4ifnums[j] > nums[j + 1]:

5nums[j], nums[j + 1] = nums[j+ 1], nums[j]

6returnnums

7print(bubble_sort([45, 32, 8, 33, 12, 22, 19,97]))

8#输出：[8, 12, 19, 22, 32,33, 45, 97]

Swift

1func bubbleSort(_ nums: inout [Int]) {

2let n = nums.count

3for i in 0..

4for j in 0..

5if nums[j]>nums[j + 1] {

6nums.swapAt(j, j +1)

7}

8}

9}

10print(nums)

11}

12

13var nums = [1,3,7,8,9]

14bubbleSort(&nums)

C++排序

1#include

2usingnamespacestd;

3template

4//整数或浮点数皆可使用

5voidbubble_sort(T arr[],intlen)

6{

7inti,j; T temp;

8for(i =; i

9for(j =; j

10if(arr[j] > arr[j +1])

11{

12temp = arr[j];

13arr[j] = arr[j +1];

14arr[j +1] = temp;

15}

16}

17intmain()

18{

19intarr[] = {61,17,29,22,34,60,72,21,50,1,62};

20intlen = (int)sizeof(arr) /sizeof(*arr);

21bubble_sort(arr, len);

22for(inti =; i

23cout

24cout

25floatarrf[] = {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};

26len = (int)sizeof(arrf) /sizeof(*arrf);

27bubble_sort(arrf, len);

28for(inti =; i

29cout

30return;

31}

RUBY

1defbubbleSort(array)

2returnarrayifarray.size

3(array.size - 2).downto(0) do |i|

4(0 .. i).each do |j|

5array[j], array[j + 1] = array[j + 1], array[j]ifarray[j] >= array[j + 1]

6end

7end

8returnarray

9end

PHP

1functionbubbleSort(\$numbers) {

2\$cnt=count(\$numbers);

3for(\$i= 0;\$i

4for(\$j= 0;\$j

5if(\$numbers[\$j] >\$numbers[\$j+ 1]) {

6\$temp=\$numbers[\$j];

7\$numbers[\$j] =\$numbers[\$j+ 1];

8\$numbers[\$j+ 1] =\$temp;

9}

10}

11}

12

13return\$numbers;

14}

15

16\$num=array(20, 40, 60, 80, 30, 70, 90, 10, 50, 0);

17var_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#语言

1namespace数组排序

2{

3classProgram

4{

5staticvoidMain(string[] args)

6{

7inttemp =;

8int[] arr = {23,44,66,76,98,11,3,9,7};

9#region该段与排序无关

10Console.WriteLine("排序前的数组：");

11foreach(intiteminarr)

12{

13Console.Write(item +"");

14}

15Console.WriteLine();

16#endregion

17for(inti =; i

18{

19#region将大的数字移到数组的arr.Length-1-i

20for(intj =; j

21{

22if(arr[j] > arr[j +1])

23{

24temp = arr[j +1];

25arr[j +1] = arr[j];

26arr[j] = temp;

27}

28}

29#endregion

30}

31Console.WriteLine("排序后的数组：");

32foreach(intiteminarr)

33{

34Console.Write(item+"");

35}

36Console.WriteLine();

38}

39}

40}

Erlang

1bubble_sort(L)->

2bubble_sort(L,length(L)).

3

4bubble_sort(L,0)->

5L;

6bubble_sort(L,N)->

7bubble_sort(do_bubble_sort(L),N-1).

8

9do_bubble_sort([A])->

10[A];

11do_bubble_sort([A,B|R])->

12caseA

13true->[A|do_bubble_sort([B|R])];

14false->[B|do_bubble_sort([A|R])]

15end.

C语言

1#include

2#defineSIZE 8

3

4voidbubble_sort(inta[],intn);

5

6voidbubble_sort(inta[],intn)

7{

8inti, j,temp;

9for(j =; j

10for(i =; i

11{

12if(a[i] > a[i +1])

13{

14temp = a[i];

15a[i] = a[i +1];

16a[i +1] =temp;

17}

18}

19}

20

21intmain()

22{

23intnumber[SIZE] = {95,45,15,78,84,51,24,12};

24inti;

25bubble_sort(number, SIZE);

26for(i =; i

27{

28printf("%d\n", number[i]);

29}

30}

31JavaScript

32function bubbleSort(arr) {

33vari = arr.length, j;

34vartempExchangVal;

35while(i >) {

36for(j =; j

37if(arr[j] > arr[j +1]) {

38tempExchangVal = arr[j];

39arr[j] = arr[j +1];

40arr[j +1] = tempExchangVal;

41}

42}

43i--;

44}

45returnarr;

46}

47

48vararr = [3,2,4,9,1,5,7,6,8];

49vararrSorted = bubbleSort(arr);

50console.log(arrSorted);

Visual Basic语言

1Submaopao()

2Dima =Array(233,10086,31,15,213,5201314,427)

3DimiAsInteger, jAsInteger

4

5Fori =UBound(a) -1ToStep-1

6Forj =Toi

7Ifa(j) > a(j +1)Then

8a(j) = a(j) + a(j +1)

9a(j +1) = a(j) - a(j +1)

10a(j) = a(j) - a(j +1)

11EndIf

12Nextj

13Nexti

14Fori =ToUBound(a)

15Printa(i)

16Nexti

17EndSub

Objective-C

1for(inti =;i

2for(intj =; j

3NSInteger left =[result[j] integerValue];

4NSInteger right =[result[j+1] integerValue];

5if(left

6[resultexchangeObjectAtIndex:j withObjectAtIndex:j+1];

7}

8}

9}

10NSLog(@"%@",result);

Go语言

GO语言2

PASCAL

1var

2a:array[1..4]ofinteger;

3i, j, temp, n:integer;

4begin

7fori :=1tondo

8forj :=1ton-ido

9ifa[j]> a[j +1]then

10begin

11temp := a[j];

12a[j] := a[j +1];

13a[j+1] := temp;

14end;

15fori:=1tondowrite(a[i]);

16end.

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180628G02S1E00?refer=cp_1026
• 腾讯「云+社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 yunjia_community@tencent.com 删除。

2018-08-04

2018-06-26

2018-06-19

2018-05-22

2018-04-12

2022-01-24