冒泡排序

在冒泡排序中,将数组的每个元素与其相邻元素进行比较,此算法处理传递中的列表。 具有n个元素的列表需要n-1次传递以进行排序。 考虑一个n个元素的数组A,其元素将使用冒泡排序进行排序。算法处理如下。

在第1遍时,A[0]A[1]进行比较,A[1]A[2]进行比较,A[2]A[3]进行比较,依此类推。 在第1遍结束时,列表的最大元素放在列表的最高索引处。

在第2遍时,A[0]A[1]进行比较,A[1]A[2]进行比较,依此类推。 在第2遍结束时,列表的第二大元素位于列表的第二高索引处。
在通过n-1遍时,A[0]A[1]进行比较,A[1]A[2]进行比较,依此类推。 在这个传球结束时。列表的最小元素放在列表的第一个索引处。

算法:

第1步 : 重复第2步,从i = 0 到 N-1
第2步 : 重复 J = i + 1 到 N - I
第3步 : IF A[J] > A[i]
        SWAP A[J] 和 A[i]
        [结束内循环]
    [结束外循环]
第4步 : EXIT

复杂度

情况 复杂度
空间 O(1)
最坏情况运行时间 O(n^2)
平均情况运行时间 O(n)
最好情况运行时间 O(n^2)

C语言实现冒泡排序算法 -

#include<stdio.h>  
void main ()  
{  
    int i, j,temp;   
    int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};   
    for(i = 0; i<10; i++)  
    {  
        for(j = i+1; j<10; j++)  
        {  
            if(a[j] > a[i])  
            {  
                temp = a[i];  
                a[i] = a[j];  
                a[j] = temp;   
            }   
        }   
    }   
    printf("Printing Sorted Element List ...\n");  
    for(i = 0; i<10; i++)  
    {  
        printf("%d\n",a[i]);  
    }  
}

执行上面示例代码,得到以下结果 -

Printing Sorted Element List . . . 
7
9
10
12
23
34
34
44
78 
101

C++语言实现冒泡排序算法 -

#include<iostream>  
using namespace std;  
int main ()  
{  
    int i, j,temp;   
    int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};   
    for(i = 0; i<10; i++)  
    {  
        for(j = i+1; j<10; j++)  
        {  
            if(a[j] < a[i])  
            {  
                temp = a[i];  
                a[i] = a[j];  
                a[j] = temp;   
            }   
        }   
    }   
    cout <<"Printing Sorted Element List ...\n";  
    for(i = 0; i<10; i++)  
    {  
        cout <<a[i]<<"\n";  
    }  
    return 0;  
}

执行上面示例代码,得到以下结果 -

Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101

Java语言实现冒泡排序算法 -

public class BubbleSort {  
    public static void main(String[] args) {  
    int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};  
    for(int i=0;i<10;i++)  
    {  
        for (int j=0;j<10;j++)  
        {  
            if(a[i]<a[j])  
            {  
                int temp = a[i];  
                a[i]=a[j];  
                a[j] = temp;   
            }  
        }  
    }  
    System.out.println("Printing Sorted List ...");  
    for(int i=0;i<10;i++)  
    {  
        System.out.println(a[i]);  
    }  
}  
}

执行上面示例代码,得到以下结果 -

Printing Sorted List . . . 
7
9
10
12
23
34
34
44
78 
101

C#语言实现冒泡排序算法 -

using System;  

public class Program  
{  
    public static void Main()  
    {  
        int i, j,temp;   
    int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};   
    for(i = 0; i<10; i++)  
    {  
        for(j = i+1; j<10; j++)  
        {  
            if(a[j] > a[i])  
            {  
                temp = a[i];  
                a[i] = a[j];  
                a[j] = temp;   
            }   
        }   
    }   
    Console.WriteLine("Printing Sorted Element List ...\n");  
    for(i = 0; i<10; i++)  
    {  
        Console.WriteLine(a[i]);  
    }  
    }  
}

执行上面示例代码,得到以下结果 -

Printing Sorted Element List . . . 
7
9
10
12
23
34
34
44
78 
101

Python语言实现冒泡排序算法 -

a=[10, 9, 7, 101, 23, 44, 12, 78, 34, 23]  
for i in range(0,len(a)):  
    for j in range(i+1,len(a)):  
        if a[j]<a[i]:  
            temp = a[j]  
            a[j]=a[i]  
            a[i]=temp  
print("Printing Sorted Element List...")  
for i in a:   
    print(i)

执行上面示例代码,得到以下结果 -

Printing Sorted Element List . . . 
7
9
10
12
23
34
34
44
78 
101

JavaScript语言实现冒泡排序算法 -

<script>   
    var a = [10, 9, 7, 101, 23, 44, 12, 78, 34, 23];  
    for(i=0;i<10;i++)  
    {  
        for (j=0;j<10;j++)  
        {  
            if(a[i]<a[j])  
            {  
                 temp = a[i];  
                a[i]=a[j];  
                a[j] = temp;   
            }  
        }  
    }  
    txt = "<br>";  
    document.writeln("Printing Sorted Element List ..."+txt);  
    for(i=0;i<10;i++)  
    {  
        document.writeln(a[i]);  
        document.writeln(txt);  
    }  
    </script>

执行上面示例代码,得到以下结果 -

Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101

PHP语言实现冒泡排序算法 -

<?php  
    $a = array(10, 9, 7, 101, 23, 44, 12, 78, 34, 23);  
    for($i=0;$i<10;$i++)  
    {  
        for ($j=0;$j<10;$j++)  
        {  
            if($a[$i]<$a[$j])  
            {  
                 $temp = $a[$i];  
                $a[$i]=$a[$j];  
                $a[$j] = $temp;   
            }  
        }  
    }  
    echo "Printing Sorted Element List ...\n";  
    for($i=0;$i<10;$i++)  
    {  
        echo $a[$i];  
        echo "\n";  
    }  
?>

执行上面示例代码,得到以下结果 -

Printing Sorted Element List ...
7
9
10
12
23
23
34
44
78
101

上一篇: 二进制(二分查找)搜索 下一篇: 桶排序