# 氣泡排序

``````第1步 : 重複第2步，從i = 0 到 N-1

SWAP A[J] 和 A[i]
[結束內迴圈]
[結束外迴圈]

``````

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
``````