冒泡排序——不同语言

写了也不少了,冒泡排序真的比较重要,推荐下不同语言的冒泡排序的写法。(下面复制于百度百科)

时间复杂度

若文件的初始状态是正序的,一趟扫描即可完成排序。所需的关键字比较次数
 和记录移动次数
 均达到最小值:
 ,
 。
所以,冒泡排序最好的 时间复杂度
 。
  若初始文件是反序的,需要进行
 趟排序。每趟排序要进行
 次关键字的比较(1≤i≤n-1),且每次比较都必须移动记录三次来达到交换记录位置。在这种情况下,比较和移动次数均达到最大值:
冒泡排序的最坏时间复杂度为
 。
综上,因此冒泡排序总的平均时间复杂度为
 。

算法稳定性

冒泡排序就是把小的元素往前调或者把大的元素往后调。比较是相邻的两个元素比较,交换也发生在这两个元素之间。所以,如果两个元素相等,我想你是不会再无聊地把他们俩交换一下的;如果两个相等的元素没有相邻,那么即使通过前面的两两交换把两个相邻起来,这时候也不会交换,所以相同元素的前后顺序并没有改变,所以冒泡排序是一种稳定排序算法。

3算法描述

C++排序

#include <iostream>using namespace std;template<typename T> //整数或浮点数皆可使用,若要使用物件时必须设定大于的运算子功能void bubble_sort(T arr[], int len) { int i, j; T temp; for (i = 0; i < len - 1; i++) for (j = 0; j < len - 1 - i; j++) if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; }}int main() { 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 = (int) sizeof(arrf) / sizeof(*arrf); bubble_sort(arrf, len); for (int i = 0; i < len; i++) cout << arrf[i] << ' '; return 0;}

RUBY

1
2
3
4
5
6
7
8
9
def  bubbleSort(array)
     return  array  if  array.size <  2
     (array.size -  2 ).downto( 0 do  |i|
         ( 0  .. i). each  do  |j|
             array[j], array[j +  1 ] = array[j +  1 ], array[j]  if  array[j] >= array[j +  1 ]
         end
     end
     return  array
end

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function  bubbleSort( $numbers ) {
     $cnt  count ( $numbers );
     for  ( $i  = 0;  $i  $cnt $i ++) {
         for  ( $j  = 0;  $j  $cnt  $i  - 1;  $j ++) {
             if  ( $numbers [ $j ] >  $numbers [ $j  + 1]) {
                 $temp  $numbers [ $j ];
                 $numbers [ $j ] =  $numbers [ $j  + 1];
                 $numbers [ $j  + 1] =  $temp ;
             }
         }
     }
 
     return  $numbers ;
}
 
$num  array (20, 40, 60, 80, 30, 70, 90, 10, 50, 0);
var_dump(bubbleSort( $num ));
 
//输出结果如下:
//array(10) {
//  [0]=>
//  int(0)
//  [1]=>
//  int(10)
//  [2]=>
//  int(20)
//  [3]=>
//  int(30)
//  [4]=>
//  int(40)
//  [5]=>
//  int(50)
//  [6]=>
//  int(60)
//  [7]=>
//  int(70)
//  [8]=>
//  int(80)
//  [9]=>
//  int(90)
//}

C#语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
冒泡算法C#
namespace  数组排序
{
     class  Program
     {
         static  void  Main( string [] args)
         {
             int  temp = 0;
             int [] arr = {23, 44, 66, 76, 98, 11, 3, 9, 7};
             #region该段与排序无关
             Console.WriteLine( "排序前的数组:" );
             foreach  (intiteminarr)
             {
                 Console.Write(item +  "" );
             }
             Console.WriteLine();
             #endregion
             for  ( int  i = 0; i < arr.Length - 1; i++)
             {
                 #region将大的数字移到数组的arr.Length-1-i
                 for  ( int  j = 0; j < arr.Length - 1 - i; j++)
                 {
                     if  (arr[j] > arr[j + 1])
                     {
                         temp = arr[j + 1];
                         arr[j + 1] = arr[j];
                         arr[j] = temp;
                     }
                 }
             #endregion
             }
             Console.WriteLine( "排序后的数组:" );
             foreach  ( int  item  in  arr)
             {
                 Console.Write(item+ "" );
             }
             Console.WriteLine();
             Console.ReadKey();
         }
     }
}

Erlang

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bubble_sort(L)->
bubble_sort(L,length(L)).
 
bubble_sort(L,0)->
L;
bubble_sort(L,N)->
bubble_sort(do_bubble_sort(L),N-1).
 
do_bubble_sort([A])->
[A];
do_bubble_sort([A,B|R])->
caseA< Bof
true->[A|do_bubble_sort([B|R])];
false->[B|do_bubble_sort([A|R])]
end .

C语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#define SIZE 8
 
void  bubble_sort( int  a[],  int  n);
 
void  bubble_sort( int  a[],  int  n)
{
     int  i, j, temp;
     for  (j = 0; j < n - 1; j++)
         for  (i = 0; i < n - 1 - j; i++)
         {
             if (a[i] > a[i + 1])
             {
                 temp = a[i];
                 a[i] = a[i + 1];
                 a[i + 1] = temp;
             }
         }
}
 
int  main()
{
     int  number[SIZE] = {95, 45, 15, 78, 84, 51, 24, 12};
     int  i;
     bubble_sort(number, SIZE);
     for  (i = 0; i < SIZE; i++)
     {
         printf ( "%d" , number[i]);
     }
     printf ( "\n" );
}

JAVA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class BubbleSort
{
     public void sort(int[] a)
     {
         int temp = 0;
         for (int i = a.length - 1; i > 0; --i)
         {
             for (int j = 0; j < i; ++j)
             {
                 if (a[j + 1] < a[j])
                 {
                     temp = a[j];
                     a[j] = a[j + 1];
                     a[j + 1] = temp;
                 }
             }
         }
     }
}

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function bubbleSort(arr) {
     var i = arr.length, j;
     var tempExchangVal;
     while (i > 0) {
         for (j = 0; j < i - 1; j++) {
             if (arr[j] > arr[j + 1]) {
                 tempExchangVal = arr[j];
                 arr[j] = arr[j + 1];
                 arr[j + 1] = tempExchangVal;
             }
         }
         i--;
     }
     return arr;
}
 
var arr = [3, 2, 4, 9, 1, 5, 7, 6, 8];
var arrSorted = bubbleSort(arr);
console.log(arrSorted);
alert(arrSorted);
控制台将输出:[1, 2, 3, 4, 5, 6, 7, 8, 9]
并且弹窗;

Visual Basic语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Sub  maopao()
     Dim  a = Array(233, 10086, 31, 15, 213, 5201314, 427)
     Dim  As  Integer , j  As  Integer
     
     For  i = UBound(a) - 1  To  Step  -1
         For  j = 0  To  i
             If  a(j) > a(j + 1)  Then
                 a(j) = a(j) + a(j + 1)
                 a(j + 1) = a(j) - a(j + 1)
                 a(j) = a(j) - a(j + 1)
             End  If
         Next  j
     Next  i
     For  i = 0  To  UBound(a)
         Print a(i)
     Next  i
End  Sub

Objective-C

(void)bubbleSort:(NSMutableArray *)array{ int i, y; BOOL bFinish = YES; for (i = 1; i<= [array count] && bFinish; i++) { bFinish = NO; for (y = (int)[array count]-1; y>=i; y--) { if ([[array objectAtIndex:y] intValue] < [[array objectAtIndex:y-1] intValue]) { [array exchangeObjectAtIndex:y-1 withObjectAtIndex:y]; bFinish = YES; } } }}Pythonalist = [1,12,2,4,51,66,45,25,96,78,55,23] def bubbleSort(aList): aLen = len(aList) for key in xrange(aLen-1): for x in xrange(aLen - key -1): if aList[x] > aList[x+1]: aList[x], aList[x+1] = aList[x+1], aList[x] print(alist)bubbleSort(alist)print(alist)

Go语言

package mainimport ("fmt")const (LENGTH=8){ func main() { var tmp intnumber:=[]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

Gofunc 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var
     a:array[1..4] of integer;
     i, j, temp, n:integer;
begin
    read(n);
    for  i := 1 to n  do  read(a[i]);
    for  i := 1 to n  do
        for  j := 1 to n-i  do
            if  a[j] > a[j + 1] then
                begin
                    temp := a[j];
                    a[j] := a[j + 1];
                    a[j+1] := temp;
                end;
     for  i:= 1 to n  do  write(a[i]);
end.

Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def  bubble(bubbleList):
     listLength  =  len (bubbleList)
     while  listLength >  0 :
         for  in  range (listLength  -  1 ):
             if  bubbleList[i] > bubbleList[i + 1 ]:
                 bubbleList[i]  =  bubbleList[i]  +  bubbleList[i + 1 ]
                 bubbleList[i + 1 =  bubbleList[i]  -  bubbleList[i + 1 ]
                 bubbleList[i]  =  bubbleList[i]  -  bubbleList[i + 1 ]
         listLength  - =  1
     print  bubbleList
 
if  __name__  = =  '__main__' :
     bubbleList  =  [ 3 4 1 2 5 8 0 ]
     bubble(bubbleList)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值