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

Posted rw助你成功

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了各编程语言冒泡排序的实现相关的知识,希望对你有一定的参考价值。

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

JAVA语言

 

 1     publicstaticvoid 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     }

 

 Python

 

 1def bubble(bubbleList):

 2     listLength = len(bubbleList)

 3     whilelistLength > 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

11if__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]

16for 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]

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

 

1def 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

7print(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>

 2usingnamespace std;

 3 template<typename T>

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

 5void 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 }

17int 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     return0;

31 }

 

 

RUBY

 

1def 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] ifarray[j] >= array[j + 1]

6        end

7    end

8     return array

9 end

 

 

php

 

 1function 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);

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 {

 3     classProgram

 4     {

 5         staticvoid 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

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

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

15end.

 

 

C语言

 

 1 #include <stdio.h>

 2#define SIZE 8

 3 

 4void bubble_sort(int a[], int n);

 5 

 6void 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 

21int 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 

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

49var arrSorted = bubbleSort(arr);

50 console.log(arrSorted);

51 alert(arrSorted);

 

 

控制台将输出:[1, 2, 3, 4, 5, 6, 7, 8, 9]
并且弹窗;

 

Visual Basic语言

 

 1Sub maopao()

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

 3     Dim i AsInteger, j AsInteger

 4    

 5     For i = UBound(a) - 1To0Step -1

 6         For j = 0To 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            EndIf

12        Next j

13     Next i

14     For i = 0ToUBound(a)

15        Print a(i)

16     Next i

17EndSub

 

 

Objective-C

 

 1for (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                 [resultexchangeObjectAtIndex:j withObjectAtIndex:j+1];

 7             }

 8         }

 9     }

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

 

 

Go语言

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

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

 

 1var

 2     a:array[1..4] of integer;

 3     i, j, temp, n:integer;

 4begin

 5    read(n);

 6    for i := 1to n do read(a[i]);

 7    for i := 1to n do

 8        for j := 1to 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:= 1to n do write(a[i]);

16end.

 

 

 

 


以上是关于各编程语言冒泡排序的实现的主要内容,如果未能解决你的问题,请参考以下文章

九大排序算法(Python 语言实现)

排序算法——冒泡排序(C语言实现)

Go语言实现冒泡排序选择排序快速排序及插入排序的方法

用Java实现冒泡排序和Arrays排序

shell脚本编程之冒泡排序脚本实现(解释非常详细,涉及正则表达式)

用C语言编写函数,要实现快速排序算法或者冒泡法